处理错误
2 分钟阅读
简要概述
向服务发送 API 请求,可能会产生多种不同的错误响应,以下文档将会描述数据结构以及大致的原因。
响应体结构
查看完整的 proto 文件定义,关键部分如下:
// ErrorResponse 用于定义统一错误响应体
message ErrorResponse {
Status error = 1;
}
// Status 用于定义统一错误状态码
message Status {
int32 code = 1;
string status = 2;
string message = 3;
repeated google.protobuf.Any details = 4;
}
一个 json 示例:
{
"error": {
"code": 5,
"status": "NotFound",
"message": "unknown service",
"details": [
{
"@type": "type.googleapis.com/grpc_kit.api.known.status.v1.TracingRequest",
"id": "b17f76dc51de4098bc974e5f2009c097"
}
]
}
}
状态码说明
code | status | http code | default message |
---|---|---|---|
0 | OK | 200 | No error. |
1 | Canceled | 499 | Request cancelled by the client. |
2 | Unknown | 500 | Unknown server error. |
3 | InvalidArgument | 400 | Client specified an invalid argument. |
4 | DeadlineExceeded | 504 | Request deadline exceeded. |
5 | NotFound | 404 | A specified resource is not found, or the request is rejected by undisclosed reasons, such as whitelisting. |
6 | AlreadyExists | 409 | The resource that a client tried to create already exists. |
7 | PermissionDenied | 403 | Client does not have sufficient permission. |
8 | ResourceExhausted | 429 | Either out of resource quota or reaching rate limiting. |
9 | FailedPrecondition | 400 | Request can not be executed in the current system state, such as deleting a non-empty directory. |
10 | Aborted | 409 | Concurrency conflict, such as read-modify-write conflict. |
11 | OutOfRange | 400 | Client specified an invalid range. |
12 | Unimplemented | 501 | The API method not implemented or enabled by the server. |
13 | Internal | 500 | Internal server error. |
14 | Unavailable | 503 | Service unavailable. |
15 | DataLoss | 500 | Unrecoverable data loss or data corruption. |
16 | Unauthenticated | 401 | Request not authenticated due to missing, invalid, or expired OAuth token. |
204 | NoContent | 204 | Service is no additional content to send in the response content. |
最后修改 08.11.2023: docs: add 204 code (0c60495)