Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error code and array of duplicates in beautified error #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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.

None yet

3 participants