DEV Community

Stephen Solka
Stephen Solka

Posted on • Updated on

Go errors.is cool

In 1.13 go introduced a new function on the errors package. Is. This lets you ask if the error you are holding is the same as a predefined defined error or the error wraps the predefined error.

var BadThingErr = errors.New("omg something happened")

type canReturnErr = func() error

err := canReturnErr()
if errors.Is(err, BadThingErr) {
  fmt.Println("that bad thing that can happen happened")
}
Enter fullscreen mode Exit fullscreen mode

Ive been using Is since it was released but there is a small detail of the the documentation that I originally missed.

An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.

type myCoolErr struct{
  errCode int
}

func (m myCoolErr) Is(err error) bool {
  me, ok := err.(myCoolErr)
  if !ok {
    return false
  }
  return me.errCode == m.errCode
}
Enter fullscreen mode Exit fullscreen mode

This lets an error control the implementation of errors.Is when comparing equality of the error object. Neat!

How does this work?

if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
  return true
}
Enter fullscreen mode Exit fullscreen mode

https://github.com/golang/go/blob/43c6ada84c6ef47e3b61646d2f2e7f6b7264929d/src/errors/wrap.go#L39-L60

In the source code of the errors package there is a cool trick. This is asking if the err object complies with an anonymous interface if it does then the go compiler allows you to call the method on the anonymous interface!

I have seen casts and casts to interfaces but never casts to an interface defined in the cast expression.

This pattern seems most useful if your target interface is a single method and only used in one place. The risk is too high for the anonymous interface definitions to diverge if its used in multiple source locations. At that point I'd recommend defining a proper type Xxx interface

Top comments (0)