admin管理员组文章数量:1405412
I've carefully read all HTTP error/status codes, still not clear which is the appropriate to return in the following scenario:
- The request is an Ajax request, so the handling of the error situation depends on the client javascript code, preferably it will give a notification.
- At server side an unexpected error occurs (say DB operation fails), however the exception is handled server side, and appropriate error message (as string) is created there.
The 'unexpected error' implies HTTP 500, however I would like to differentiate between real server internal (and unhandled) errors and handled use cases what I've described above.
Which Http error/status code is the appropriate? Does it matter if the request a query (using GET) or some update (using POST)?
I've carefully read all HTTP error/status codes, still not clear which is the appropriate to return in the following scenario:
- The request is an Ajax request, so the handling of the error situation depends on the client javascript code, preferably it will give a notification.
- At server side an unexpected error occurs (say DB operation fails), however the exception is handled server side, and appropriate error message (as string) is created there.
The 'unexpected error' implies HTTP 500, however I would like to differentiate between real server internal (and unhandled) errors and handled use cases what I've described above.
Which Http error/status code is the appropriate? Does it matter if the request a query (using GET) or some update (using POST)?
Share Improve this question asked Jun 17, 2015 at 6:45 g.pickardoug.pickardou 36k43 gold badges154 silver badges320 bronze badges 5- 1 It doesn't matter where the request es from. – Bergi Commented Jun 17, 2015 at 7:18
- Bergi: Do you mean Ajax vs browser? – g.pickardou Commented Jun 17, 2015 at 7:26
- @g.pickardou The server doesn't care (or know) if it's an AJAX request or not. EDIT: Ideally it shouldn't know/care about that :) – Sunil D. Commented Jun 17, 2015 at 7:27
-
@g.pickardou: There's not even a difference between "ajax" and "browser" request from the server's perspective. I was rather looking for
wget
/curl
and any other HTTP-capable tool/library… – Bergi Commented Jun 17, 2015 at 7:35 - I did know the server doesn't care (or know). My question was about the client's perspective, and it would 'make sense' vs 'make no sense' to differentiate the expectation, because that's a different use case, and different user experience. – g.pickardou Commented Jun 18, 2015 at 10:35
3 Answers
Reset to default 5Both scenarios that you described are server errors. Regardless if it was a DB operation that fails or you server-side code didn't handle an error (unhandled error). Therefore, for the Front-end point of view, A server error has occurred (500 error status).
Now, differentiating an custom application error from an unhandled error, es down to which server-side technology you are using. You can decorate (add a property) your error object with some information that only your handled application errors would have. Therefore, on the Front-end, if that property is present, it's a known server error.
//500 error - unknown model
{ status: 500, exception: "my unknown error." }
//500 error - known model
{ status: 500, exception: "DB script has failed.", errorCode: 001 }
It shouldn't matter if the call is a GET or a POST. Both Methods could return the same http status code.
There is a list of available status codes. Usually if the request was valid, you'll receive the status code 200. If a resource could not be found, you'll get 404. All the server side errors will result in a 500. If you intend to differentiate between your server side errors, you'll have to catch them manually and return a response with a different status code. Note: Status code 500 is the worst of them all. You should not use it, for example, to return the error messages in case a validation has failed. Use a different status code in this case.
The appropriate response is 5xx Server Error. Depending on the type of error you may choose to use "503 Service Unavailable" or "501 Not Implemented", but in the more generic cases opt for "500 Internal Server Error" (GET or POST doesn't matter).
You may choose to provide a custom response body to differentiate between handled error messages and unhandled ones.
本文标签: javascriptWhich Http errorstatus code is the appropriateStack Overflow
版权声明:本文标题:javascript - Which Http errorstatus code is the appropriate? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744878828a2630080.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论