add tests, move CustomError to common, fix casting

This commit is contained in:
Matteo Pagliazzi
2016-03-03 00:16:28 +01:00
parent 05b6e25c28
commit 80f791c86b
11 changed files with 260 additions and 42 deletions

View File

@@ -1,8 +1,8 @@
// The error handler middleware that handles all errors
// and respond to the client
import logger from '../../libs/api-v3/logger';
import CustomError from '../../../../common/script/api-v3/customError';
import {
CustomError,
BadRequest,
InternalServerError,
} from '../../libs/api-v3/errors';
@@ -24,11 +24,17 @@ export default function errorHandler (err, req, res, next) { // eslint-disable-l
// If we can't identify it, respond with a generic 500 error
let responseErr = err instanceof CustomError ? err : null;
// TODO don't return always 400 for errors in common code
// If CustomError but without httpCode then they come from shared code, treat as 400s
if (err instanceof CustomError && !err.httpCode) {
err.httpCode = 400;
}
// 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.name = err.name;
responseErr.message = err.message;
}