REST:将应用程序错误映射到HTTP状态代码 [英] REST: Mapping application errors to HTTP Status codes

查看:78
本文介绍了REST:将应用程序错误映射到HTTP状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像这样重用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:

如果不存在1234,

GET/package/1234 返回 404找不到

GET /package/1234 returns 404 Not Found if 1234 doesn't exist

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:

POST/dispatch/?for_package = 1234 返回 412前提条件失败如果/dispatch和Package 1234都存在,则BUT 1234尚未准备好进行分发. /p>

POST /dispatch/?for_package=1234 returns 412 Precondition Failed if /dispatch and package 1234 both exist, BUT 1234 isn't ready for dispatch just yet.


( 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状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆