I have a gRPC service written with Go that plays a proxy-like role by wrapping a gRPC message it receives into another gRPC message to a separate backend service. That backend service can return errors but I don't want to simply return an error
type from the backend to the proxy service because I'm using a stream and I don't want to abort the whole stream because of a single error.
Instead, what I want to do is return a response message that contains an error field. I am currently just using a string field like this and setting the string ProxiedMessage.err
field via fmt.Sprintf("Error encountered: %v", err)
:
message ProxiedMessage {
bytes wrappedRpc = 1;
string err = 2;
}
This works for me but I am wondering if there is a "proper" way to return an error
type as a field that I should be using instead. Something like this (which doesn't work):
message ProxiedMessage {
bytes wrappedRpc = 1;
error err = 2;
}
I'd simply define an error message with whatever fields make sense to you, e.g.:
And then include that as field in the stream message:
You can also use a simple enum for that, in case you don't need rich error messages. And then you map the error code represented by the enum to some human-readable string on the client side: