错误处理
错误说明
- 错误遵循 rfc7807 规范返回错误
- 当发生服务器已知错误会返回
rfc7807错误 - rfc7807的错误响应头的
content-type为application/problem+json - 未知错误返回错误的http状态码,
content-type可能为application/json(如果有遇到这种情况请和我们联系,可能是bug导致的)
响应内容
响应头
| 参数名称 | 参数说明 | 类型 |
|---|---|---|
| content-type | 响应内容的类型,取值为: application/problem+json | string |
响应参数
| 参数名称 | 参数说明 | 类型 |
|---|---|---|
| type | 错误类型 | string |
| title | 标题 | string |
| status | 状态码 | integer(int32) |
| detail | 详细描述 | string |
| instance | 请求的路径 | string |
响应示例:
{
"type": "",
"title": "",
"status": 0,
"detail": "",
"instance": ""
}
建议的错误处理逻辑
- 调用接口判断http状态码如果是大于399的是接口调用出错了
- 获取接口的
content-type如果是application/problem+json读取body内容转成json - 如果需要根据类型判断走自定义逻辑需要获取 json 的
type判断 - 如果不需要自定义逻辑则可以直接获取 json里面的
title和detail显示出来 - 当获取到的
content-type不是application/problem+json直接返回未知错误
错误列表
授权
| status | type | title | detail |
|---|---|---|---|
| 401 | authentication-unauthorized | 未授权 | 请检查授权参数是否正确 |
| 401 | authentication-bad-secret | 未授权 | 客户端密钥错误 |
| 401 | authentication-client-not-found | 未授权 | 客户端不存在 |
| 401 | authentication-client-id-malformed | 未授权 | 客户端ID格式错误 |
| 401 | authentication-client-disabled | 未授权 | 客户端已禁用 |
| 401 | authentication-signature-time-skewed | 未授权 | 签名时间不正确 |
| 401 | authentication-signature-time-skewed | 未授权 | 签名已过期 |
| 403 | access-denied | 权限拒绝 | 权限拒绝 |
搜索
| status | type | title | detail |
|---|---|---|---|
| 400 | search-header-missing | 搜索失败 | 请求头缺少 |
| 400 | search-header-wrong-format | 搜索失败 | 请求头格式错误 |
| 403 | search-book-no-permission | 搜索失败 | 没有该书籍的权限,请联系运营人员 |
| 404 | search-book-not-found | 搜索失败 | 英汉书籍未找到 |
| 404 | search-book-not-found | 搜索失败 | 汉英书籍未找到 |
其它
| http_statue_code | 描述 |
|---|---|
| 429 | 超出请求次数限制,需要稍后再试 |
FQA
-
当在调用接口发生401错误,但是令牌没过期的情况
这是因为用户IP地址发生变动,所以遇到这种情况只需要重新获取令牌
-
接口返回
429状态码,content-type为空并且body也是空的这是因为接口在这个IP地址调用太频繁接口被限流导致的,所以错误处理的时候需要先判断是否是429错误,如果是429错误则提示用户操作慢一点