Skip to content
This repository was archived by the owner on Oct 2, 2021. It is now read-only.

Latest commit

 

History

History
30 lines (26 loc) · 911 Bytes

File metadata and controls

30 lines (26 loc) · 911 Bytes

错误处理

虽然HTTP Stauts Code4xx5xx的状态码来表示哪里出错了,但是其代表的只是HTTP协议层面的错误描述,它无法提供和业务相关的更具体错误描述。基于此种情况,我们需要设计一套描述业务层面错误的数据结构:

[
  {
    "error": "user_name",
    "message": "用户名不能为空。"
  },
  {
    "error": "age",
    "message": "用户年龄不能小于0。"
  }
]
  1. 这个数据结构仅在状态码为4xx5xx出现的时候才会使用;2xx的时候则不包含此数据结构。
  2. error字段可以是一些出错的字段名、某一错误类别(比如no_permission)等等。
    [
      {
        "error": "no_permission",
        "message": "没有user.delete的权限"
      }
    ]

参考

Problem Details for HTTP APIs : https://tools.ietf.org/html/rfc7807