add express-validator, add body parser middleware, support some more errors in error handler middleware

This commit is contained in:
Matteo Pagliazzi
2015-11-08 22:48:31 +01:00
parent 87f50ff2b9
commit 7dd6eb76c9
4 changed files with 54 additions and 3 deletions

View File

@@ -23,11 +23,20 @@ export default function errorHandler (err, req, res, next) {
// If we can't identify it, respond with a generic 500 error
let responseErr = err instanceof CustomError ? err : null;
if (!responseErr) {
// Handle errors created with 'http-errors' or similar that have a status/statusCode property
if (err.statusCode && typeof err.statusCode === 'number') {
responseErr = new CustomError();
responseErr.httpCode = err.statusCode;
responseErr.error = err.name;
responseErr.message = err.message;
}
if (!responseErr || responseErr.httpCode >= 500) {
// Try to identify the error...
// ...
// Otherwise create an InternalServerError and use it
// we don't want to leak anything, just a generic error message
// Use it also in case of identified errors but with httpCode === 500
responseErr = new InternalServerError();
}