diff --git a/typescript/bot/src/main.ts b/typescript/bot/src/main.ts index f21302067..e6e90f1bf 100644 --- a/typescript/bot/src/main.ts +++ b/typescript/bot/src/main.ts @@ -5,7 +5,7 @@ import { Client, type CommandInteraction, Intents, type SelectMenuInteraction } import { Env, ServerConfig } from "./config"; import { handleIsCommand } from "./interaction-handlers/handle-is-command"; import { GetUserAndTokenForDiscordID } from "./query/user-map"; -import { app } from "./server/server"; +import { app, MountFallbackHandlers } from "./server/server"; import { ClosePgConnection } from "./services/pg/db"; import { RegisterSlashCommands } from "./slash-commands/register"; import { VERSION_PRETTY } from "./version"; @@ -101,6 +101,9 @@ void (async () => { log.info(`Logged in successfully to ${client.guilds.cache.size} guilds.`); + // Mount the 404 + error fallbacks last. + MountFallbackHandlers(); + // Mount our express server. const server = app.listen(Env.PORT); diff --git a/typescript/bot/src/server/server.ts b/typescript/bot/src/server/server.ts index ba4a1d72d..0762bd130 100644 --- a/typescript/bot/src/server/server.ts +++ b/typescript/bot/src/server/server.ts @@ -159,18 +159,6 @@ app.post("/webhook", ValidateWebhookRequest, async (req, res) => { return res.sendStatus(statusCode); }); -/** - * 404 Handler. If something gets to this point, they haven't matched with anything. - * - * @name ALL * - */ -app.all("*", (_req, res) => - res.status(404).json({ - success: false, - description: "Nothing found here.", - }), -); - interface ExpressJSONErr extends SyntaxError { status: number; message: string; @@ -209,6 +197,20 @@ const MainExpressErrorHandler: express.ErrorRequestHandler = (err, req, res, _ne }); }; -app.use(MainExpressErrorHandler); +export function MountFallbackHandlers(): void { + /** + * 404 Handler. If something gets to this point, they haven't matched with anything. + * + * @name ALL * + */ + app.all("*", (_req, res) => + res.status(404).json({ + success: false, + description: "Nothing found here.", + }), + ); + + app.use(MainExpressErrorHandler); +} log.info(`Starting express server on port ${Env.PORT}.`);