Performant gRPC rich error handling in Go

664 Views Asked by At

Google docs propose the following model (https://cloud.google.com/apis/design/errors#error_model) for sending rich errors in gRPC but it seems that the error string is sent to the user every time. What I want to do is to send a code and then map it to a string when it reaches the client.

What I want to know is whatever the proto3 language supports writing data so that I would use it client-side, without defining a custom structure for the purposes of mapping error codes to error messages.

1

There are 1 best solutions below

0
On BEST ANSWER

In your proto definition, define a simple enum with any extra error codes:

enum extraStatusCode {
    UNKNOWN         = 0;  // not set/used
    TOO_MANY_FOOS   = 1;
    NOT_ENOUGH_BARS = 2;
}

And include it as a top-level field in any returned message:

message User {
    string uid      = 1;
    string email    = 2;

    // ...

    extraStatusCode = 15;
}

if a message is sent with a non-zero extraStatusCode - then an edge case was encountered.