3

My application accepts http DELETE requests which will remove entries from a database managed through hibernate.

The removal of certain entries would produce a hibernate.ConstraintViolationException because they are key in another table, thus they are not accepted. However, this error can change in time depending of the status of the DB.

What is the correct http response for such a scenario?

I thought of 412 (Precondition Failed) because the precondition of the entry being not in use by the system is not met.

5
  • 1
    You may want to re-read the definition of the 412 code. This status code is inadequate for your problem. Commented May 3, 2016 at 14:33
  • @DaSourcerer, I interpreted it as PRECONDITION := "the entry is not in use in other tables". I guess there are more appropriate status to return and that is why I posted the question.. So what would an appropriate status be in such a scenario? Commented May 3, 2016 at 14:43
  • Nope, the precondition is a reference to the If-* type of headers. To be more precise: it is the precondition of a conditional request issued by the client that has failed. Commented May 3, 2016 at 14:45
  • @DaSourcerer right, so what should my application return? Commented May 3, 2016 at 15:08
  • 1
    For the future: Choosing an HTTP Status Code – Stop Making It Hard. I found myself using it far more often than not ;) Commented May 3, 2016 at 15:29

2 Answers 2

3

Did you consider 409? -- "The 409 (Conflict) status code indicates that the request could not be completed due to a conflict with the current state of the target resource. This code is used in situations where the user might be able to resolve the conflict and resubmit the request. The server SHOULD generate a payload that includes enough information for a user to recognize the source of the conflict." -- https://greenbytes.de/tech/webdav/rfc7231.html#status.409

Sign up to request clarification or add additional context in comments.

3 Comments

That's compelling: I was ready to accept the 424, but now 409 sounds perfectly reasonable too. The client needs to solve the conflict, that is delete a second entry which references the original one in order to be able to request the first DELETE. I found all this rather confusing: what's the difference between a "(failed) dependency" and a "conflict"?
Yes and I believe both 409 and 429 could be suitable for the scenario described in my original question. In my actual case, I think that 409 is the correct one and that the server should inform the client which other steps (e.g. DELETEs) are required to safely perform the original DELETE. Say the server supported a recursive delete on any referring element but an error occurred (e.g. there's a protected element), 429 would be the right one, I guess, if not a 5xx.
Hm, interesting. It appears my understanding of the status codes 409 and 424 is deeply flawed.
1

This sounds a lot like the client would not have the powers to correct that error if it occurs. That alone is something that would qualify for the 5xx-range of status codes:

The 5xx (Server Error) class of status code indicates that the server is aware that it has erred or is incapable of performing the requested method. […] These response codes are applicable to any request method.

To be precise, I think 503 (Service Unavailable) is in order here. That code is mostly known for indicating maintenance, but it is really indicating a temporary state on the server side that is preventing the request to be fulfilled. This would also be in line with this answer.

If you are uncomfortable with this, here are some alternatives in order of recommendation:

However, if the client has a chance to correct this error (e.g. by issuing another request first), the 5xx-class is out of the question and you should start with code 424. If you are concerned with this code being introduced by WebDAV, don't worry: It is listed in the IANA HTTP Status Code Registry and therefor valid in HTTP.

2 Comments

424. I haven't noticed that one before. It's indeed like that: other actions such a delete of other entities are requested first. It's not really a server side error. Thanks.
424 does not fit: "The 424 (Failed Dependency) status code means that the method could not be performed on the resource because the requested action depended on another action and that action failed. For example, if a command in a PROPPATCH method fails, then, at minimum, the rest of the commands will also fail with 424 (Failed Dependency)."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.