CRUD service response on unique constraint violation #175
-
I need to match in a service the unique constraint violation error when inserting an item to a collection. I tried to add a document that violates such constraint with a call like the following: curl --location --request POST 'https://crud-service/some-collection/' \
--header 'Content-Type: application/json' \
--data-raw '{
"someUniqueField": "some-already-existing"
}' And I got the following error response: {
"statusCode": 422,
"error": "Unprocessable Entity",
"message": "E11000 duplicate key error collection: some-collection index: some_unique_index dup key: { someUniqueField: \"some-already-existing\" }"
} Is it guaranteed that a response with 422 status code is due to a unique constraint violation? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Hi @malta895, looking at the source code of the I don't know if, in the future, other cases could be added. Since the eventual addition of a new According to the MongoDB Documentation, |
Beta Was this translation helpful? Give feedback.
Hi @malta895, looking at the source code of the
crud-service
seems like right now you can get a422
only for this kind of error during an insert or bulk insert operation.I don't know if, in the future, other cases could be added.
Since the eventual addition of a new
422
response status code is not a breaking change probably you should parse themessage
.According to the MongoDB Documentation,
E11000
is the "duplicate key" error, so it should be safe (for now at least).