知识卡片
错误也是正式响应
接口出错时,消费者需要识别错误并决定下一步,而不是收到不可预测的诊断垃圾。
错误应纳入接口设计,使机器识别错误类型和恢复机会。书中用RFC 7807 Problem Details的type/title/status/detail/instance:类型URI及标题说明类别,detail和instance描述本次发生。
可返回Content-Type: application/problem+json,扩展字段要由type文档解释;必要时配Retry-After。已有媒体类型内建错误结构则优先保留该格式。普通状态码足够时无需额外正文,调试细节也不应对外泄露。本卡记录2022年使用的RFC版本,不声明它是现行最新版。
关联:[[词汇描述覆盖数据与动作]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 超媒体服务 · 查看原书依据
The key to success is to treat errors as an “alternate response” instead of a failed request. In other words, always include error reporting as a feature of your service interface, not a failure condition.
参考来源
- 位置:《RESTful Web API Patterns and Practices Cookbook》第5章《Hypermedia Services》(来源项目book-sources:extracted/RESTful Web API Patterns and Practices Cookbook Connecting and Orchestrating Microservices and Distributed Data (Mike Amundsen)/_epub-src/OEBPS/ch05.xhtml,配方5.12)
- 结论依据:原文列出Problem Details字段,区别固定问题类型与本次发生细节,并限制调试信息外泄。
- 原始内容:The key to success is to treat errors as an “alternate response” instead of a failed request. In other words, always include error reporting as a feature of your service interface, not a failure condition.