Skip to content

Commit

Permalink
Merge pull request hagopj13#53 from samkit5495/patch-2
Browse files Browse the repository at this point in the history
Handled mongoose Errors as BAD_REQUEST
  • Loading branch information
hagopj13 authored Dec 24, 2020
2 parents 8757f04 + 50e2a9c commit a37d572
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/middlewares/error.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const mongoose = require('mongoose');
const httpStatus = require('http-status');
const config = require('../config/config');
const logger = require('../config/logger');
Expand All @@ -6,9 +7,10 @@ const ApiError = require('../utils/ApiError');
const errorConverter = (err, req, res, next) => {
let error = err;
if (!(error instanceof ApiError)) {
const statusCode = error.statusCode || httpStatus.INTERNAL_SERVER_ERROR;
const statusCode =
error.statusCode || error instanceof mongoose.Error ? httpStatus.BAD_REQUEST : httpStatus.INTERNAL_SERVER_ERROR;
const message = error.message || httpStatus[statusCode];
error = new ApiError(statusCode, message, false, err.stack);
error = new ApiError(statusCode, message, !(statusCode === httpStatus.INTERNAL_SERVER_ERROR), err.stack);
}
next(error);
};
Expand Down

0 comments on commit a37d572

Please sign in to comment.