by Leon Rosenshein

Multiple Oopsies

Sometimes things go wrong. If they go wrong in unrecoverable ways it can be good stop immediately and let someone know. In Go that's typically handled by returning an Error from your function. Straightforward enough.

Sometimes tough, multiple things go wrong, but the actions you're taking are independent enough that it makes sense to keep going. What do you do then? You could keep a list and do some formatting magic or, you could use Uber's shared multierr.

The errors returned by the multierr methods implements the Error interface, so you can just drop then in wherever you need it. The benefit comes at the end when you check for/display errors. We use this in the infra CLI in many places. The one you're probably most familiar with is in infra auth refresh. If you've got multiple AWS profiles defined the tool will try to refresh all of your AWS creds. It's possible the service is down, but the more common error case is that there's a problem with one or more of your profiles. In that case we just add the error to the multierror and keep going. That way as much useful work as possible is done and all errors are noted and returned to the user. Then when everything is done we just check for an error and print it out if needed.

It's a neat pattern. Very useful when you have tracking multiple, independent streams of work. Especially if they're happening in parallel.