DEV Community

Stephen Solka
Stephen Solka

Posted on • Edited on

6 1

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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Imagine monitoring that's actually built for developers

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay