From 8b70567b9446d5791fac38f48cd1308a043dbd88 Mon Sep 17 00:00:00 2001 From: Kevin J Hoerr Date: Mon, 27 Apr 2020 02:41:40 -0400 Subject: Handle promise errors in handleStartup --- src/util/config.ts | 77 +++++++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 35 deletions(-) (limited to 'src/util/config.ts') diff --git a/src/util/config.ts b/src/util/config.ts index b3a1ead..d2ad130 100644 --- a/src/util/config.ts +++ b/src/util/config.ts @@ -37,7 +37,7 @@ export const persistTemplate = async (input: Template): Promise => { process.exit(1); } // if the output file exists, then we are fine with continuing without - logger.warning( + logger.warn( "Could not generate '%s' from template file, but file already exists: %s", input.outputFile, err1 @@ -50,43 +50,50 @@ const TARGET_URL = process.env.TARGET_URL ?? "http://localhost:3000"; const HOST_DIR = configOrError("HOST_DIR"); export const handleStartup = async (): Promise => { - await fs.promises.access(HOST_DIR, fs.constants.R_OK | fs.constants.W_OK); - if (!path.isAbsolute(HOST_DIR)) { - logger.error("HOST_DIR must be an absolute path"); - process.exit(1); - } - - const mongo = await new MongoClient(MONGO_URI, { useUnifiedTopology: true }) - .connect() - .catch((err: MongoError) => { - logger.error(err.message ?? "Unable to connect to database"); + try { + await fs.promises.access(HOST_DIR, fs.constants.R_OK | fs.constants.W_OK); + if (!path.isAbsolute(HOST_DIR)) { + logger.error("HOST_DIR must be an absolute path"); process.exit(1); - }); + } + + const mongo = await new MongoClient(MONGO_URI, { useUnifiedTopology: true }) + .connect() + .catch((err: MongoError) => { + logger.error(err.message ?? "Unable to connect to database"); + process.exit(1); + }); - await persistTemplate({ - inputFile: path.join( - __dirname, - "..", - "public", - "templates", - "bash.template" - ), - outputFile: path.join(HOST_DIR, "bash"), - context: { TARGET_URL } - } as Template); - await persistTemplate({ - inputFile: path.join( - __dirname, - "..", - "public", - "templates", - "index.html.template" - ), - outputFile: path.join(HOST_DIR, "index.html"), - context: { TARGET_URL } - } as Template); + await persistTemplate({ + inputFile: path.join( + __dirname, + "..", + "..", + "public", + "templates", + "bash.template" + ), + outputFile: path.join(HOST_DIR, "bash"), + context: { TARGET_URL } + } as Template); + await persistTemplate({ + inputFile: path.join( + __dirname, + "..", + "..", + "public", + "templates", + "index.html.template" + ), + outputFile: path.join(HOST_DIR, "index.html"), + context: { TARGET_URL } + } as Template); - return mongo; + return mongo; + } catch (err) { + logger.error("Error occurred during startup: %s", err); + process.exit(1); + } }; export const handleShutdown = (mongo: MongoClient, server: Server) => ( -- cgit