json-server icon indicating copy to clipboard operation
json-server copied to clipboard

Http Status codes on repost should be 409 and not 500

Open madsh opened this issue 3 years ago • 2 comments

Hi,

What a great tool! and what a list of issues... :) I will add one to the queue, since I am not sure if you accept pull request or agree on the solution.

When I post elements without id the browser accepts them and assigns an id. Just as expected.

When I post elements having an 'id' the browser accepts them and stores them under the id. Just as expected.

However, when I repost an element with an id all ready in the collection I get HTTP 500 Error: Insert failed, duplicate id. Not what I expected.

I will suggest replacing the error code with HTTP 409 Conflict: Insert failed, duplicate id.

The main argument is that 5xx is server errors that the client can not fix. But re-posting is a client behaviour problem, and can be easily fixed by sending with another id, or leave it without.

Thanks for a great piece of software!

madsh avatar Nov 30 '21 20:11 madsh

Hi, I would like to contribute for this issue. Could you assign it to me?

mgncodes avatar Dec 11 '21 09:12 mgncodes

The exception actually bubbles up from lodash.createId but somebody could wrap the call in src/server/plural.js in a try/catch and potentially emit the 409 status code instead.

ericnewton76 avatar May 12 '22 15:05 ericnewton76