Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Error Handling in MERN MATERIAL DEMO APP
const getErrorMessage = err => {
let message = '';
if (err.code) {
switch (err.code) {
case 11000:
case 11001:
message = getUniqueErrorMessage(err);
break;
default:
message = 'Something went wrong.';
}
} else {
for (let errName in err.errors) {
if (err.errors[errName].message) message = err.errors[errName].message;
}
}
return message;
};
const getUniqueErrorMessage = err => {
let output;
try {
let fieldName = err.message.substring(
err.message.lastIndexOf('.$') + 2,
err.message.lastIndexOf('_1')
);
output =
fieldName.charAt(0).toUpperCase() +
fieldName.slice(1) +
' already exists';
} catch (ex) {
output = 'Field Already Exists!';
}
return output;
};
export default { getErrorMessage };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment