The validation constraints added to the user schema fields will throw error messages, if violated when user data is saved to the database. To handle these validation errors and other errors that the database may throw when we make queries to it, we will define a helper method to return a relevant error message that can be propagated in the request-response cycle as appropriate.
We will add the getErrorMessage helper method in the server/helpers/dbErrorHandler.js file. This method will parse and return the error message associated with the specific validation error or other error that occurred while querying MongoDB using Mongoose.
mern-skeleton/server/helpers/dbErrorHandler.js:
const getErrorMessage = (err) => { ...