Skip to content

Conversation

Igor-Lopes
Copy link

It would be easier to have the error code (E11000 or E11001) in the beautified error JSON, as well, an array of duplicates fields right away. In my Express App, in every Insert or update I try to catch mongoose errors. If it's a duplicate error, I have to give the duplicate fields in the response, otherwise, I throw a 500 error and log de request. Here's a beautified error example with the changes:

{
	"errors": {
		"email": {
			"message": "Validator failed for path `email` with value `[email protected]`",
			"name": "ValidatorError",
			"properties": {
				"type": "Duplicate value",
				"path": "email",
				"value": "[email protected]"
			},
			"kind": "Duplicate value",
			"path": "email",
			"value": "[email protected]"
		}
	},
	"message": "Validation failed",
	"name": "ValidationError",
	"code": 11000,
	"fields": [
		"email"
	]
}

@matteodelabre
Copy link
Owner

Hello @Igor-Lopes, thanks for your contribution, and sorry for the delay in answering! I totally agree on the usefulness of a code attribute. However, the data in your proposed fields attribute can easily be computed using Object.keys(err.errors).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants