From 50e2a9c37f3609c129033aee0f556e80ca4cbc50 Mon Sep 17 00:00:00 2001 From: Samkit Jain Date: Sat, 19 Dec 2020 14:14:55 +0530 Subject: [PATCH] Handled mongoose Errors as BAD_REQUEST --- src/middlewares/error.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/middlewares/error.js b/src/middlewares/error.js index e224a7ed..98d1bd8d 100644 --- a/src/middlewares/error.js +++ b/src/middlewares/error.js @@ -1,3 +1,4 @@ +const mongoose = require('mongoose'); const httpStatus = require('http-status'); const config = require('../config/config'); const logger = require('../config/logger'); @@ -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); };