Package google.rpc
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
状态
Status
类型定义了适用于不同编程环境(包括 REST API 和 RPC API)的逻辑错误模型。此类型供 gRPC 使用。设计错误模型的目的是:
概览
Status
消息包含三项数据:错误代码、错误消息和错误详细信息。错误代码应为 google.rpc.Code
的枚举值,但如果需要,也可以接受其他错误代码。错误消息应为向开发者显示的英语版消息,旨在帮助开发者理解并解决相关错误。understandunderstand如果需要提供向用户显示的本地化版错误消息,可以将本地化后的消息放入错误详细信息中,也可以在客户端中将该消息本地化。错误详细信息是可选项,其中可包含错误的任意相关信息。google.rpc
软件包中包含一组预定义的错误详细信息类型,可用于常见的错误情况。
语言映射
Status
消息是错误模型的逻辑表示形式,但不一定是实际的传输格式。当 Status
消息在不同的客户端库和不同的传输协议中公开时,可以采用不同的方式映射。例如,此消息可能映射到 Java 中的某些异常,但更可能映射到 C 中的某些错误代码。
其他用途
错误模型和 Status
消息可用于各种环境(无论是否使用 API),以便在不同环境中提供一致的开发者体验。
下面是此错误模型的一些示例运用:
部分错误。如果服务需要将部分错误返回到客户端,则可以将 Status
嵌入正常响应中,以指示部分错误。
工作流错误。一个典型的工作流包含多个步骤。每个步骤都有一条用于报告错误的 Status
消息。
批量操作。如果客户端使用批量请求和批量响应,则应直接在批量响应中使用 Status
消息,使每个错误子响应与一条消息对应。
异步操作。如果 API 调用将异步操作结果嵌入其响应中,则应直接使用 Status
消息表示这些操作的状态。
日志记录。如果某些 API 错误存储在日志中,则在为确保安全/隐私而执行任何必要的删除操作之后,可以直接使用消息 Status
。
字段 |
code |
int32
状态代码,应是 google.rpc.Code 的枚举值。
|
message |
string
面向开发者的错误消息(应采用英语)。任何向用户显示的错误消息都应进行本地化并通过 google.rpc.Status.details 字段发送,或者由客户端进行本地化。
|
details[] |
Any
包含错误详细信息的消息列表。有一组通用的消息类型可供 API 使用。
|
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThe \u003ccode\u003eStatus\u003c/code\u003e type defines a consistent and flexible error model suitable for various programming environments, including REST APIs and RPC APIs.\u003c/p\u003e\n"],["\u003cp\u003eIt provides three key pieces of information: an error code using \u003ccode\u003egoogle.rpc.Code\u003c/code\u003e, a developer-facing English error message, and optional error details for context.\u003c/p\u003e\n"],["\u003cp\u003eWhile logically represented by the \u003ccode\u003eStatus\u003c/code\u003e message, its actual implementation may vary across languages and wire protocols.\u003c/p\u003e\n"],["\u003cp\u003eThis error model can be utilized in diverse scenarios beyond APIs to ensure a uniform developer experience, including partial errors, workflow errors, batch operations, and asynchronous operations.\u003c/p\u003e\n"]]],["The `Status` message defines an error model with three data pieces: `code`, `message`, and `details`. The `code` is an error code, `message` is a developer-facing English error message, and `details` contains additional error information. This model can be used for partial, workflow, batch, and asynchronous operations and in logging. It is designed for simplicity and flexibility and is adaptable across different programming environments and API types, with a mapping system that can change based on implementation.\n"],null,["# Package google.rpc\n\nIndex\n-----\n\n- [Status](/assistant/sdk/reference/rpc/google.rpc#google.rpc.Status) (message)\n\nStatus\n------\n\nThe `Status` type defines a logical error model that is suitable for different programming environments, including REST APIs and RPC APIs. It is used by [gRPC](https://github.com/grpc). The error model is designed to be:\n\n- Simple to use and understand for most users\n- Flexible enough to meet unexpected needs\n\n#### Overview\n\nThe `Status` message contains three pieces of data: error code, error message, and error details. The error code should be an enum value of `google.rpc.Code`, but it may accept additional error codes if needed. The error message should be a developer-facing English message that helps developers *understand* and *resolve* the error. If a localized user-facing error message is needed, put the localized message in the error details or localize it in the client. The optional error details may contain arbitrary information about the error. There is a predefined set of error detail types in the package `google.rpc` that can be used for common error conditions.\n\n#### Language mapping\n\nThe `Status` message is the logical representation of the error model, but it is not necessarily the actual wire format. When the `Status` message is exposed in different client libraries and different wire protocols, it can be mapped differently. For example, it will likely be mapped to some exceptions in Java, but more likely mapped to some error codes in C.\n\n#### Other uses\n\nThe error model and the `Status` message can be used in a variety of environments, either with or without APIs, to provide a consistent developer experience across different environments.\n\nExample uses of this error model include:\n\n- Partial errors. If a service needs to return partial errors to the client, it may embed the `Status` in the normal response to indicate the partial errors.\n\n- Workflow errors. A typical workflow has multiple steps. Each step may have a `Status` message for error reporting.\n\n- Batch operations. If a client uses batch request and batch response, the `Status` message should be used directly inside batch response, one for each error sub-response.\n\n- Asynchronous operations. If an API call embeds asynchronous operation results in its response, the status of those operations should be represented directly using the `Status` message.\n\n- Logging. If some API errors are stored in logs, the message `Status` could be used directly after any stripping needed for security/privacy reasons.\n\n| Fields ||\n|-------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `code` | `int32` The status code, which should be an enum value of `google.rpc.Code`. |\n| `message` | `string` A developer-facing error message, which should be in English. Any user-facing error message should be localized and sent in the [google.rpc.Status.details](/assistant/sdk/reference/rpc/google.rpc#google.rpc.Status.FIELDS.repeated.google.protobuf.Any.google.rpc.Status.details) field, or localized by the client. |\n| `details[]` | [Any](https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.Any) A list of messages that carry the error details. There is a common set of message types for APIs to use. |"]]