A cursory look at the article, shows that the most important observation about error handling in Go is missing.<p>Errors should be "decorated" (wrapped, contextualized...) in 99% of the cases.<p>In the end you get errors that describe step by step what your program tried to do and why it failed, for example:<p>* could not load profile: could not open file: permission denied.<p>* could not download profile image: could not open URL: HTTP GET failed: network is down.<p>This has many advantages:<p>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.)<p>2. Errors are still easy to grep in code to work out the program flow (the stack trace, basically.)<p>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.<p>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.<p>Overall, I think this is one of the best approaches at error handling.
I appreciated the article, but the error handling in Go just bugs me. So verbose...<p>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?<p><a href="https://play.golang.org/p/j-oXsZz51ki" rel="nofollow">https://play.golang.org/p/j-oXsZz51ki</a>
I wish more developers could do "investigation" like this for a new languages they learn.<p>For me, the main difference between Go's way of handling language and the rest of mainstream languages is that it makes error handling <i></i>unmagical<i></i>.<p>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.<p>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.<p>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)
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.<p>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.<p>The two options I've seen in the wild are:<p>1. Create a constant for a given error, like:<p><pre><code> var ErrFetchForbidden = errors.New("FETCH_FORBIDDEN")
</code></pre>
Then the calling function can do:<p><pre><code> if err == ErrFetchForbidden {
return 403
} else if err == ErrFetchNotFound {
return 404
} else {
return 500
}
</code></pre>
2. Create a custom type for your error like so:<p><pre><code> type ErrFetchForbidden string
</code></pre>
this has the benefit that the errorer can put more specific info into the error besides the Error() string.<p><pre><code> var err ErrFetchForbidden = "error retrieving the user object"
return err
</code></pre>
and then the caller can switch on type<p><pre><code> switch v := err.(type) {
case ErrFetchForbidden:
return 403
case ErrFetchNotFound:
return 404
default:
return 500
}
</code></pre>
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?
> The other bit of good news is that you can't unknowingly ignore a returned error, like you can with an unchecked exception. The compiler will force you at a minimum to declare the error as _, and tools like errcheck do a good job of keeping you honest.<p>Actually, we unknowingly ignore returned errors much more often than we think, like when we call a function and opt out of assigning any of the return values to variables. Consider this function, which returns a single value (being an error).<p><pre><code> func Failure() error {...}
</code></pre>
You can always choose to call an error-returning function without declaring any placeholder (`_`):<p><pre><code> Failure()
</code></pre>
There are several commonly used functions that return errors that are regularly ignored. How about `io.Writer`?<p><pre><code> writer.Write([]byte("Hello")) // returns (n int, err error)
</code></pre>
It's quite common to call that function without feeling a need to check on the bytes written or a possible error. Or, consider whether you consistently check the return values of `fmt.Println()`, which also returns `(n int, err error)`...
For everybody who is interested in improving his error handling skills:<p><a href="https://dave.cheney.net/2016/04/27/dont-just-check-errors-handle-them-gracefully" rel="nofollow">https://dave.cheney.net/2016/04/27/dont-just-check-errors-ha...</a>
if err != nil
return err<p>if err != nil
return err<p>if err != nil
return err<p><a href="https://github.com/docker/cli/search?q=%22if+err+%21%3D+nil%22&unscoped_q=%22if+err+%21%3D+nil%22" rel="nofollow">https://github.com/docker/cli/search?q=%22if+err+%21%3D+nil%...</a><p><a href="https://github.com/kubernetes/kubernetes/search?q=%22if+err+%21%3D+nil%22&unscoped_q=%22if+err+%21%3D+nil%22" rel="nofollow">https://github.com/kubernetes/kubernetes/search?q=%22if+err+...</a><p><a href="https://github.com/coreos/etcd/search?q=%22return+err%22&unscoped_q=%22return+err%22" rel="nofollow">https://github.com/coreos/etcd/search?q=%22return+err%22&uns...</a><p><a href="https://github.com/influxdata/influxdb/search?q=%22if+err+%21%3D+nil%22&unscoped_q=%22if+err+%21%3D+nil%22" rel="nofollow">https://github.com/influxdata/influxdb/search?q=%22if+err+%2...</a><p>The reality of Go's error handling is that you just implement exactly what exception bubbling does painfully by hand.
The first time I used Go on a big project, error handling bugged me enough that I wrote a package that let me use errors like exceptions:<p><a href="https://github.com/s4y/go-exc" rel="nofollow">https://github.com/s4y/go-exc</a><p>I’m not sure if I’d use it again today, but it was a fun exercise.