知识卡片
错误状态码表达处理结果
接口错误要同时给出准确状态码和可处理的错误说明。把失败藏在 2xx 响应体里,会误导依赖状态码的客户端;5xx 可能触发自动重试,所以非幂等操作尤其要明确“请求是否已生效”的语义,避免把未知结果当成确定失败。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- · 查看原书依据
An accurate status code must be provided to the consumer, because often consumers will build logic around the status code provided in the response. We have seen many APIs that return errors in the body along with a 2xx type of response, which is used to indicate success.
参考来源
- 位置:《Mastering API Architecture》第1章《Design, Build, and Specify APIs》之《Error Handling》(源文件:_epub-src/OEBPS/ch01.xhtml)
- 结论依据:原文指出消费者会按状态码编写逻辑,批评用 2xx 承载错误,并以支付服务的 500 说明失败响应不一定能表明业务动作是否已发生。
- 原始内容:An accurate status code must be provided to the consumer, because often consumers will build logic around the status code provided in the response. We have seen many APIs that return errors in the body along with a 2xx type of response, which is used to indicate success.