From 69334bab8e159994bd649749dac227fb2be03246 Mon Sep 17 00:00:00 2001 From: Christian Fehmer Date: Tue, 13 Aug 2024 08:52:46 +0300 Subject: impr: log unknown validation errors (@fehmer) --- backend/src/api/routes/index.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/backend/src/api/routes/index.ts b/backend/src/api/routes/index.ts index 86eec0bc5..287b53f61 100644 --- a/backend/src/api/routes/index.ts +++ b/backend/src/api/routes/index.ts @@ -77,7 +77,7 @@ export function addApiRoutes(app: Application): void { function applyTsRestApiRoutes(app: IRouter): void { createExpressEndpoints(contract, router, app, { jsonQuery: true, - requestValidationErrorHandler(err, _req, res, next) { + requestValidationErrorHandler(err, req, res, _next) { let message: string | undefined = undefined; let validationErrors: string[] | undefined = undefined; @@ -90,16 +90,24 @@ function applyTsRestApiRoutes(app: IRouter): void { } else if (err.body?.issues !== undefined) { message = "Invalid request data schema"; validationErrors = err.body.issues.map(prettyErrorMessage); - } - - if (message !== undefined) { - res - .status(422) - .json({ message, validationErrors } as MonkeyValidationError); + } else if (err.headers?.issues !== undefined) { + message = "Invalid header schema"; + validationErrors = err.headers.issues.map(prettyErrorMessage); } else { - next(); + Logger.error( + `Unknown validation error for ${req.method} ${ + req.path + }: ${JSON.stringify(err)}` + ); + res + .status(500) + .json({ message: "Unknown validation error. Contact support." }); return; } + + res + .status(422) + .json({ message, validationErrors } as MonkeyValidationError); }, globalMiddleware: [authenticateTsRestRequest()], }); -- cgit v1.2.3