简体   繁体   中英

Which HTTP Response Status to Return

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

  • If a database error occurs
  • When writing to response with response.getWriter() if IOException occurs.

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

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...

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, eg 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.

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 :)

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM