问题描述
像这样重用RFC HTTP状态代码是否被认为是良好实践,还是我们应该构建能准确映射的新代码? >我们特定的错误原因?
Is it to be considered good practice to reuse RFC HTTP Status codes like this, or should we be making up new ones that map exactly to our specific error reasons?
我们正在围绕几个旧版应用程序设计一个Web服务API.
We're designing a web service API around a couple of legacy applications.
除了响应正文中的JSON/XML数据结构外,我们的目标是返回对Web缓存和开发人员有意义的HTTP状态代码.
In addition to JSON/XML data structures in the Response Body, we aim to return HTTP Status Codes that make sense to web caches and developers.
但是您如何将不同类别的错误映射到适当的HTTP状态代码?团队中的每个人都同意以下几点:
But how do you go about mapping different classes of errors onto appropriate HTTP Status codes? Everyone on the team agrees on the following:
GET/package/1234/next_checkpoint 如果 400错误请求,则返回 400错误请求. 和 1234有效,但是这里的next_checkpont没有意义...
GET /package/1234/next_checkpoint returns 400 Bad Request if "next_checkpoint" and 1234 are valid to ask for but next_checkpont here doesn't make sense...
,依此类推...等等,但是在某些情况下,除了"400"和"400" -例如:
and so on... but, in some cases, things needs to be more specific than just "400" - for example:
( HTTP/1.1中的状态代码和 WebDAV扩展程序中的状态代码. )
推荐答案
REST式使用HTTP意味着必须保持API统一.这意味着您不能添加特定于域的方法(ala GET_STOCK_QUOTE),但也意味着您无法添加特定于域的错误代码(ala 499产品缺货).
RESTful use of HTTP means that you must keep the API uniform. This means that you cannot add domain specific methods (ala GET_STOCK_QUOTE) but it also means that you cannot add domain specific error codes (ala 499 Product Out Of Stock).
实际上,HTTP客户端错误代码是很好的设计检查,因为如果正确设计资源语义,HTTP错误代码的含义将正确表示任何错误.如果您认为需要其他错误代码,则您的资源设计可能是错误的.
In fact, the HTTP client error codes are a good design check because if you design your resource semantics properly, the HTTP error code meanings will correctly express any errors. If you feel you need additional error codes, your resource design is likely wrong.
Jan
这篇关于REST:将应用程序错误映射到HTTP状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!