aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChristian Fehmer <[email protected]>2024-08-13 08:52:46 +0300
committerChristian Fehmer <[email protected]>2024-08-13 08:52:56 +0300
commit69334bab8e159994bd649749dac227fb2be03246 (patch)
tree5f223916d981290c6815018a7349a924b54552df
parent19cef8b4af4e48a8be85ea22137ac12818751912 (diff)
downloadmonkeytype-feature/validation-error-handling.tar.gz
monkeytype-feature/validation-error-handling.zip
impr: log unknown validation errors (@fehmer)feature/validation-error-handling
-rw-r--r--backend/src/api/routes/index.ts24
1 files 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()],
});