Skip to content

Commit

Permalink
Handled mongoose Errors as BAD_REQUEST
Browse files Browse the repository at this point in the history
  • Loading branch information
samkit5495 authored Dec 19, 2020
1 parent a53bd90 commit 50e2a9c
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 50e2a9c

Please sign in to comment.