In the application, I have defined two models. The first model is the movie model, and the second model is the user model. In the movie model, movie name is a unique field, and in the user model, email is a unique field. In the “duplicateKeyErrorHandler” function, the message is static, but I need a model name for a duplicate movie or duplicate email.
Duplicate error message is written in “duplicateKeyErrorHandler” function.
I am getting the same error message for duplicate movie names and duplicate user emails, but I need different messages for different models and unique fields.
Now error messages for duplicate movie and duplicate user email are same:
“message”: “There is already a movie with name The Matrix. Please use another name.”,
Expected messages should be like this:
for duplicate movie name
“message”: “There is already a movie with name ‘The Matrix’. Please use another name.”,
for duplicate user email
“message”: “There is already a email with this email [email protected]. Please use another email.”,
Error controller code is:
const CustomError = require('../utils/CustomError');
const castErrorHandler = (err) => {
const msg = `Invalid value for ${err.path}: ${err.value}`;
return new CustomError(msg, 400);
}
const duplicateKeyErrorHandler = (err) => {
const msg = `There is already a movie with name ${err.keyValue.name}. Please use another name.`;
return new CustomError(msg, 400);
}
const devErrors = (res, error) => {
res.status(error.statusCode).json({
status: error.status,
message: error.message,
stackTrace: error.stack,
error: error
});
}
const prodErrors = (res, error) => {
if (error.isOperational) {
res.status(error.statusCode).json({
status: error.status,
message: error.message
});
}
else {
res.status(500).json({
status: 'error',
message: 'Something went wrong! Please try again later.'
});
}
}
module.exports = (error, req, res, next) => {
error.statusCode = error.statusCode || 500;
error.status = error.status || 'error';
if (process.env.NODE_ENV === "development") {
if(error.name === "CastError") error = castErrorHandler(error);
if(error.code === 11000) error = duplicateKeyErrorHandler(error);
devErrors(res, error);
} else if (process.env.NODE_ENV === "production") {
if(error.name === "CastError") error = castErrorHandler(error);
if(error.code === 11000) error = duplicateKeyErrorHandler(error);
prodErrors(res, error);
}
};
Duplicate field error response is:
{
"status": "error",
"message": "E11000 duplicate key error collection: node_practice_1.users index: email_1 dup key: { email: "[email protected]" }",
"stackTrace": "MongoServerError: E11000 duplicate key error collection: node_practice_1.users index: email_1 dup key: { email: "[email protected]" }n at InsertOneOperation.execute (D:\RND\Node-JS\node-practice-1\node_modules\mongodb\lib\operations\insert.js:51:19)n at process.processTicksAndRejections (node:internal/process/task_queues:95:5)n at async executeOperation (D:\RND\Node-JS\node-practice-1\node_modules\mongodb\lib\operations\execute_operation.js:136:16)n at async Collection.insertOne (D:\RND\Node-JS\node-practice-1\node_modules\mongodb\lib\collection.js:155:16)",
"error": {
"errorResponse": {
"index": 0,
"code": 11000,
"errmsg": "E11000 duplicate key error collection: node_practice_1.users index: email_1 dup key: { email: "[email protected]" }",
"keyPattern": {
"email": 1
},
"keyValue": {
"email": "[email protected]"
}
},
"index": 0,
"code": 11000,
"keyPattern": {
"email": 1
},
"keyValue": {
"email": "[email protected]"
},
"statusCode": 500,
"status": "error"
}
}