mirror of
https://github.com/jeffvli/sonixd.git
synced 2026-04-29 02:32:37 -04:00
Add readable validation error
This commit is contained in:
@@ -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,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user