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

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay