Advanced Error Handling in Golang

If you have ever written any Golang code you have probably noticed the built-in error type interface. Golang uses error values to indicate an abnormal state. The error type represents any value that can describe itself as a string. Here is the interface’s declaration:

type error interface {
    Error() string

The most commonly-used error implementation is the errors package’s implementation that allows you to instantiate errors by using the following code snippet:

err: = errors.New("http: operation is not allowed")

However, the error handling is so simplified that does not provide us with information about where the error occurred in the source code. This can cause some difficulties in tracing errors in your application log. Also, there are some particular situations that you have multiple errors that you want to correlate with particular operation step.

We can still have all that by just adopting a few Golang third-party packages in our application.

Stack tracing errors with status codes

The errorx is a package that provides an error interface implementation inspired by PostgreSQL styleguide.

By installing it with the following command:

$ go get

It provides us with stack trace, error codes and nesting of errors:

if err := step(); err != nil {
  errx := errorx.New(http.StatusInternalServerError, "Operation failed")

Handling multiple errors

The go-multierror is a package for Go that provides a mechanism for representing a list of error values as a single error.

Let’s first install it:

$ go get

Then we can use it in the following way:

var result error

if err := step1(); err != nil {
    result = multierror.Append(result, err)
if err := step2(); err != nil {
    result = multierror.Append(result, err)

return result

Published by in programming languages and tagged #go using 284 words.