错误处理
在调用接口的过程中,可能出现下列几种错误情况:
服务器维护中,
503状态码HTTP/1.1 503 Service Unavailable Retry-After: 3600 Content-Length: 41 {"message": "Service In the maintenance"}发送了无法转化的请求体,
400状态码HTTP/1.1 400 Bad Request Content-Length: 35 {"message": "Problems parsing JSON"}服务到期(比如付费的增值服务等),
403状态码HTTP/1.1 403 Forbidden Content-Length: 29 {"message": "Service expired"}因为某些原因不允许访问(比如被 ban ),
403状态码HTTP/1.1 403 Forbidden Content-Length: 29 {"message": "Account blocked"}权限不够,
403状态码HTTP/1.1 403 Forbidden Content-Length: 31 {"message": "Permission denied"}需要修改的资源不存在,
404状态码HTTP/1.1 404 Not Found Content-Length: 32 {"message": "Resource not found"}缺少了必要的头信息,
428状态码HTTP/1.1 428 Precondition Required Content-Length: 35 {"message": "Header User-Agent is required"}发送了非法的资源,
422状态码HTTP/1.1 422 Unprocessable Entity Content-Length: 149 { "message": "Validation Failed", "errors": [ { "resource": "Issue", "field": "title", "code": "missing_field" } ] }
所有的 error 哈希表都有 resource, field, code 字段,以便于定位错误,code 字段则用于表示错误类型:
missing: 说明某个字段的值代表的资源不存在invalid: 某个字段的值非法,接口文档中会提供相应的信息missing_field: 缺失某个必须的字段already_exist: 发送的资源中的某个字段的值和服务器中已有的某个资源冲突,常见于某些值全局唯一的字段,比如 @ 用的用户名(这个错误我有纠结,因为其实有 409 状态码可以表示,但是在修改某个资源时,很一般显然请求中不止是一种错误,如果是 409 的话,多种错误的场景就不合适了)