From 3c7e2e3ac2c9a9a9a4cea1ed04601d3707b35c4b Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Tue, 23 Feb 2021 09:43:12 +0000 Subject: [PATCH] Cleaned up requires in boot error handling - moved logging variable outside of the try-catch block, so it doesn't need a 2nd require - I was considering having config,logging etc shared components required in a function together, but it's less readable - Having a try-catch for loading shared components before main boot is also confusing - This is simplest and most readable IMO --- core/boot.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/core/boot.js b/core/boot.js index 6f430dd5ef..ba2a6c881d 100644 --- a/core/boot.js +++ b/core/boot.js @@ -216,7 +216,10 @@ async function bootGhost() { // Metrics const startTime = Date.now(); debug('Begin Boot'); + + // We need access to these variables in both the try and catch block let ghostServer; + let logging; try { // Config must be the first thing we do, because it is required for absolutely everything @@ -224,19 +227,18 @@ async function bootGhost() { const config = require('./shared/config'); debug('End: Load config'); + // Logging is used absolutely everywhere + debug('Begin: Load logging'); + logging = require('./shared/logging'); + const bootLogger = new BootLogger(logging, startTime); + debug('End: Load logging'); + // Version is required by sentry & Migratior config & so is fundamental to booting - // However, it involves reading package.json so its slow - // It's here for visibility on slowness + // However, it involves reading package.json so its slow & it's here for visibility on that slowness debug('Begin: Load version info'); require('./server/lib/ghost-version'); debug('End: Load version info'); - // Logging is used absolutely everywhere - debug('Begin: Load logging'); - const logging = require('./shared/logging'); - const bootLogger = new BootLogger(logging, startTime); - debug('End: Load logging'); - // Sentry must be initialised early, but requires config debug('Begin: Load sentry'); require('./shared/sentry'); @@ -283,11 +285,9 @@ async function bootGhost() { return ghostServer; } catch (error) { const errors = require('@tryghost/errors'); - // @TODO: fix this extra require - const logging = require('./shared/logging'); + // Ensure the error we have is an ignition error let serverStartError = error; - if (!errors.utils.isIgnitionError(serverStartError)) { serverStartError = new errors.GhostError({message: serverStartError.message, err: serverStartError}); }