Exploring Error Handling Patterns in Go

By trptcolin - a day ago

Showing first level comment(s)

A cursory look at the article, shows that the most important observation about error handling in Go is missing.

Errors should be "decorated" (wrapped, contextualized...) in 99% of the cases.

In the end you get errors that describe step by step what your program tried to do and why it failed, for example:

* could not load profile: could not open file: permission denied.

* could not download profile image: could not open URL: HTTP GET failed: network is down.

This has many advantages:

1. Much more readable than stack traces (especially if they include source file and line information or exception class names: users don't care about those.)

2. Errors are still easy to grep in code to work out the program flow (the stack trace, basically.)

3. When reading the code, you can see from the error context strings what the code is actually doing. Basically it serves a function of comments and (unlike comments) error strings remain up to date.

It is definitely verbose, especially the not equal nil part, as it's a result of Go attempt not to have special cases. Also it's a pity that errors can be silently ignored: maybe Go2 could be stricter here.

Overall, I think this is one of the best approaches at error handling.

dullgiulio - 6 hours ago

What is not covered here, and what I'm still searching for a good pattern for, is being able to return different errors depending on the type of failure.

Suppose you have a function that fetches a model from your database. It can return an error if the given user doesn't have permission to fetch this model, or it can return an error if your db connection barfs for some reason. The calling function needs to be able to differentiate between the two errors. Most of what I've read on the subject makes it seem like people prefer to only ever check if err != nil.

The two options I've seen in the wild are:

1. Create a constant for a given error, like:

  var ErrFetchForbidden = errors.New("FETCH_FORBIDDEN")
Then the calling function can do:

  if err == ErrFetchForbidden {
    return 403
  } else if err == ErrFetchNotFound {
    return 404
  } else {
    return 500
  }
2. Create a custom type for your error like so:

  type ErrFetchForbidden string
this has the benefit that the errorer can put more specific info into the error besides the Error() string.

  var err ErrFetchForbidden = "error retrieving the user object"
  return err
and then the caller can switch on type

  switch v := err.(type) {
    case ErrFetchForbidden:
      return 403
    case ErrFetchNotFound:
      return 404
    default:
      return 500
  }
We've gone with option 2 for now, (wrapping them with the pkg/errors package) because it seems simpler. Anyone else have good patterns for handling this?

macrael - 3 hours ago

I appreciated the article, but the error handling in Go just bugs me. So verbose...

The built-in tool does not even warn about unused errors... not `go build`, and not `go vet`. What's more important, an ignored error or an unused import?

https://play.golang.org/p/j-oXsZz51ki

tln - a day ago

For everybody who is interested in improving his error handling skills:

https://dave.cheney.net/2016/04/27/dont-just-check-errors-ha...

arendtio - 2 hours ago

I wish more developers could do "investigation" like this for a new languages they learn.

For me, the main difference between Go's way of handling language and the rest of mainstream languages is that it makes error handling unmagical.

It literally says – errors are just like any other return values. Let's say, if you have function `sqrt` and return a value, and then call this function – you probably is interested in this return value and should handle it somehow (or mute with `_`). Now, the same applies for errors - if function returns the error, you likely to think how to handle it – do something in place or propagate up the stack.

There is also a cultural moment to this. As we mostly learn by examples, and most Go code has proper error checks (not equal to "proper error handling" but nevertheless), it makes newcomers to do the same as well, even while disagreeing with Go's way. I've heard from many devs that Go was the reason that made them appreciate proper error handling.

And honestly, I feel this too, and I think the reason is that in Go it's too easy to "handle errors properly". I never had this feeling with languages with exceptions, where I had to read whole books (!) just to learn how to properly use them and be confident in the way how I handle errors. (this is just an example, not the spark to start return values vs exceptions battle, just in case)

divan - 7 hours ago

if err != nil return err

if err != nil return err

if err != nil return err

https://github.com/docker/cli/search?q=%22if+err+%21%3D+nil%...

https://github.com/kubernetes/kubernetes/search?q=%22if+err+...

https://github.com/coreos/etcd/search?q=%22return+err%22&uns...

https://github.com/influxdata/influxdb/search?q=%22if+err+%2...

The reality of Go's error handling is that you just implement exactly what exception bubbling does painfully by hand.

rco8786 - 8 hours ago