Add readable validation error

This commit is contained in:
jeffvli
2022-05-19 01:49:19 -07:00
parent 7efcf10755
commit 3696ebbcf4
2 changed files with 14 additions and 2 deletions

View File

@@ -1,5 +1,7 @@
import { NextFunction, Request, Response } from 'express';
import { isJsonString } from '../utils';
const errorHandler = (
err: any,
req: Request,
@@ -10,7 +12,9 @@ const errorHandler = (
statusCode: err.statusCode || 500,
response: 'Error',
error: {
message: JSON.parse(err.message) || err.message,
message: isJsonString(err.message)
? JSON.parse(err.message)
: err.message,
path: req.path,
},
});

View File

@@ -42,7 +42,15 @@ export const validateRequest: <TParams = any, TQuery = any, TBody = any>(
}
if (errors.length > 0) {
throw ApiError.badRequest(JSON.stringify(errors));
throw ApiError.badRequest(
JSON.stringify(
[
`[${errors[0].type}]`,
`[${errors[0].errors.issues[0].path[0]}]`,
errors[0].errors.issues[0].message,
].join(' ')
)
);
}
return next();
};