From c62907102cd967b202ce25a939a08f0bb870ee08 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Sun, 27 Aug 2023 19:27:44 -0600 Subject: [PATCH] Prevent HTTP 500 when the server can't load the word list --- src/endpoints/new_game.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/endpoints/new_game.ts b/src/endpoints/new_game.ts index 099e679..031e361 100644 --- a/src/endpoints/new_game.ts +++ b/src/endpoints/new_game.ts @@ -28,7 +28,12 @@ const route: ServerRoute = { if (config.game.files[word_list] == null) { throw boom.notAcceptable(`Invalid word list`); }; - let phrases = readFileSync(config.game.files[word_list] as string, `utf-8`).split(`\n`); + let phrases: string[]; + try { + phrases = readFileSync(config.game.files[word_list] as string, `utf-8`).split(`\n`); + } catch { + return `Couldn't load word list "${word_list}", please try a different one`; + }; let phrase = phrases[Math.floor(Math.random() * phrases.length)].trim(); log.info(`New game in ${channel} with answer: ${phrase}`);