## 錯誤處理
在調用接口的過程中,可能出現下列幾種錯誤情況:
* 服務器維護中,`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 的話,多種錯誤的場景就不合適了)