Handling errors in gRPC and go-kit services

Travis Jeffery
HackerNoon.com
Published in
6 min readNov 2, 2017

--

If you look up how to handle custom errors in gRPC/go-kit services you will probably find a few sketchy instructions but no system. A frustrating omission since custom errors are useful and common in Go projects. Here’s my attempt.

  1. Define an error message in protobuf with the fields/data needed within and across the services.
  2. Implement Go’s error interface for the generated error so it can be passed around like other errors in Go.
  3. Support stack traces so you can see the source of an error and understand how it came to be.
  4. Respond with the error in a field on the response thereby exposing the error to other services.

Define a useful error message in protobuf

What makes for a useful error? To start, our error needs a message like “user not found” and a code like 404. Additionally, nested errors work for situations like validations, where an error consists of subordinates — where the message could be something like “invalid user” with nested errors {“email”: “must be set”, “password”: “must be stronger”}. A slice of strings is used to catch all other details — for instance if you wanted a localized, user-facing message; put it in the details.

Implement the error interface

By implementing the error interface, you can pass around and use the error like any other error handled writing Go. You’ll want your functions to return your custom error as the error interface too, otherwise you’ll hit this confusing behavior: Why is my nil error value not equal to nil?

It’s a good idea for functions that return errors always to use the error type in their signature (as we did above) rather than a concrete type such as *MyError, to help guarantee the error is created correctly. As an example,os.Open returns an error even though, if not nil, it's always of concrete type *os.PathError.

I’ve read opinions on the web, mostly from the Google camp, saying compiled protobuf code should be kept separate from your own handwritten code, making this API impossible in Go. Instead you’d define an error in protobuf, define an error in your code, and convert one to the other as needed. But in practice handwritten code alongside the protobufs has not posed a problem and benefits one with APIs such as this and other useful APIs like getters for commonly retrieved nested fields. So go ahead I say.

Support stack traces

Stack traces make it easy to find the source of the errors and understand how they came to be.

I used gogo’s protobuf fork to add the stack field with a custom type, which isn’t supported by the official Golang protobuf. The field being public is annoying and I don’t want its contents marshaled/unmarshaled — the former I can’t do anything about because protobuf gets pissy over private fields, the latter I can fix by implementing the marshaler/unmarshaler interfaces to nop.

I stole the code for populating and printing the stack traces from upspin, for the most part anyway. The stack is populated when the errors are created with the WrapErr and E functions and printed with the Error() function. If you wanted an error without a stack trace then you’d instantiate one yourself.

WrapErr is similar to Dave Cheney’s errors.Wrap, taking an error and wrapping it with a message. I use WrapErr at the lowest level of my code to annotate errors returned by the stdlib or a third-party pkg. E takes in a list of args of various types and instantiates a corresponding error, for example E(“user not found”, 404) would match the string to the Error’s message and the int to its code.

Respond with an error field

Errors are exposed to other services by putting an error field on each response. At the edges of the service, the endpoints, the error’s type is asserted and set on the response. If you have a HTTP service acting as a gateway to your gRPC service, you can use the RPC’s error code to set the HTTP response’s status code.

See it all together

I’ve pushed an example to GitHub so you can check everything out.

This workflow works swell but if you got opinions on something better I’d like to hear them.

Why not metadata/return the error to gRPC

I tried writing an API where I’d return the error to gRPC rather than setting it on the response. Middleware on the server would encode the error to metadata and middleware on the client would decode the error and return it, making the gRPC call work exactly like a local function call.

The problem is go-kit doesn’t give you an opportunity to encode the error to the context. If your endpoint returns an error go-kit will send it straight to gRPC which will only know to take the Error() string of the error, missing the rest of the data. The downside of not supporting this is your client has to check for two errors when it makes a request, the returned error and the request error. I’d prefer go-kit support it, but it seems it’s not gonna happen.

Please say hi at @travisjeffery.

Hit the 👏 and share if you found this useful.

Thanks for reading.

--

--

Travis Jeffery
HackerNoon.com

Working on Kafka/Confluent. Made software at Basecamp, Segment. Writing open-source software https://github.com/travisjeffery.