Skip to content

Commit

Permalink
properly return error responses when errors are caught
Browse files Browse the repository at this point in the history
  • Loading branch information
ssundahlTTD committed Nov 7, 2024
1 parent 5ef9879 commit fd31cb8
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express, { RequestHandler } from 'express';
import { PhoneNumberFormat, PhoneNumberUtil } from 'google-libphonenumber';
import Handlebars from 'hbs';
import createError from 'http-errors';
import i18n from 'i18n';
import { z } from 'zod';

Expand Down Expand Up @@ -118,13 +119,6 @@ const handleOptoutSubmit: RequestHandler<{}, { message: string } | { error: stri
res.render('confirmation', { message : '' });
};

const steps: Record<string, RequestHandler> = {
/* eslint-disable quote-props */
'email_prompt': handleEmailPromptSubmission,
'optout_submit' : handleOptoutSubmit,
/* eslint-enable quote-props */
} as const;

/* GET home page. */
router.get('/', (_req, res, _next) => {
res.render('index', {
Expand All @@ -133,32 +127,37 @@ router.get('/', (_req, res, _next) => {
});
});

enum Step {
'email_prompt' = 'email_prompt',
'optout_submit' = 'optout_submit',
}

const stepHandlers: Record<Step, RequestHandler> = {
[Step.email_prompt]: handleEmailPromptSubmission,
[Step.optout_submit]: handleOptoutSubmit,
};

const DefaultRouteRequest = z.object({
step: z.string(),
step: z.nativeEnum(Step).optional(),
});

const defaultRouteHandler: RequestHandler<{}, {}, z.infer<typeof DefaultRouteRequest>> = async (req, res, next) => {
let step;

let requestStep: Step | undefined;
try {
step = DefaultRouteRequest.parse(req.body).step;
requestStep = DefaultRouteRequest.parse(req.body).step;
} catch (e) {
logger.log('error', 'error while parsing the request');
return;
}

if (!step) {
logger.log('error', 'no step');
next(createError(500));
return;
}

const handler = Object.prototype.hasOwnProperty.call(steps, step) && steps[step];
if (!handler) {
logger.log('error', `invalid step ${step}`);
return;
if (requestStep) {
const handler = stepHandlers[requestStep];
await handler(req, res, next);
} else {
logger.log('error', 'no step');
next(createError(500));
}

await handler(req, res, next);
};

router.post('/', defaultRouteHandler);
Expand Down

0 comments on commit fd31cb8

Please sign in to comment.