GRPC - Error Handling

发布时间 2023-11-17 16:22:58作者: ZhangZhihuiAAA

If an error occurs, gRPC returns two basic pieces of information: a status code and an optional error message that explains the problem in detail. 

 

Status codes
gRPC uses predefined status codes within the RPC protocol that are understood among different languages. For example, for successful operations, gRPC returns an OK status code. All the remaining codes are about unsuccessful use cases:
CANCELLED—In this use case, the client calls the server, and for a specific reason, it cancels the request. For example, you call multiple services, and for whichever returns first, you use that data and cancel the other requests.
 INVALID_ARGUMENT—This status code is caused by the caller in that it provides invalid input, and the server complains about that. For example, the server will return this status code if you provide an empty order ID during payment creation.
 DEADLINE_EXCEEDED—This status code shows that the deadline expired before the actual operation could complete. For example, say you configure your client to have a deadline of 5 seconds. If you call an endpoint with this client and it takes 6 seconds to complete, you will see it will get DEADLINE_EXCEEDED after 5 seconds before the actual operation finishes.
 NOT_FOUND—This status code states that a resource is not found. For example, you want to get order details by ID, but you get NOT_FOUND since there is no order with that ID.
 ALREADY_EXISTS—This status code is for preventing resource duplication. For example, if you try creating a user with an existing email address, the server will return this status code.
PERMISSION_DENIED—If the operation is not allowed for the current caller, the server will return this status code. You might be already logged into the system, but the resource you are trying to access may need higher permissions.
RESOURCE_EXHAUSTED—This code is used once the caller reaches its limit for usage. For example, you may have a quota for a Software as a Service (SaaS) product; then, once you reach the limit for the product in that environment, the server will return this status code.
INTERNAL—This status code is used for internal server errors.