Structured errors in Go

19 points by runxiyu


StevenAndrewCoffman

Hot take. But if you squint a bit, errors are basically contexts but they operate on the ascending side of the call tree, not the descending side.

Contexts accrue information as they descend the call tree and errors accrue information as they ascend the call tree.

This means that, if you want to store structured metadata in a context, that information is only useful if you pass it to an error so that metadata can ride the call tree ascension like a rollercoaster all the way up to the top where it’s actually useful.

This was the most thought-provoking part of the article! It reminds me of how a Zig style Error Return Trace are more useful in figuring out problems in how an expected error was handled (flow control) and surfaced to the user.

See https://github.com/bracesdev/errtrace

southclaws

oh it’s me!

we’ve been using these ideas/library in prod for a few years now, some rough updated thoughts:

luckily i kept it below v1 so at some point i’ll break the api and finish all those improvements i’ve been wanting in prod!

antoinewdg

We converged on more or less the same ideas at $WORK (where we also write an HTTP API in Go), it’s validating to see we are not the only ones!

The only difference is that when we’re attaching the context to an error we’re also attaching a stack trace to the error and as a result don’t bother with wrapping errors.