kamaci
kamaci

Reputation: 75257

Which HTTP Response Status to Return

I have a REST operation. User wants to delete an object.

Which status code(500, 503 etc.) should I return to the client side.(I mean which one is more convenient?)

Upvotes: 2

Views: 2954

Answers (4)

Darrel Miller
Darrel Miller

Reputation: 142252

You have not provided us with sufficient information to be able to answer the question regarding database errors. If the database error is caused due to violating a referential integrity constraint then you should be returning a 400 error because it is an error on the part of the client.

What kind of database errors are you concerned about?

Upvotes: 0

Spyder
Spyder

Reputation: 1902

If it's a temporary error, 503 is appropriate as it generally means "retry later". I don't think most database errors fall into that category though. In most cases a generic 500 is the best response. There aren't many options in the 500 list:

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_Server_Error

If it's an error due to the delete being an invalid operation or something like that, 403 forbidden is what I'd use :)

Upvotes: 0

nos
nos

Reputation: 229342

There isn't that many 500 codes, and most of them have a meaning other than "database failed"/"ioexception occured". Note that you should probably distinguish between what kind of database error, e.g. deleting a non existant object (remember to check the "affected rows" of your DELETE statement), would result in a 404 status.

For your two examples, you should return status 500. Though if an IO exception occur on the response.getWriter(), it's likely too late to return an error, or you can't reach the client anyhow.

And, take a look at the response codes used by the twitter api as well as this page.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503769

500 sounds most appropriate here, unless you know it's due to server overload.

If response.getWriter() (or a later call to the writer) throws an IOException, then I suspect you're beyond the stage of being able to usefully affect the response received by the client anyway...

Upvotes: 3

Related Questions