https://go.dev/blog/go1.13-errors
https://pkg.go.dev/errors#As

Don’t:

1
2
3
func isPairingNotFound(err error) bool {
return err.(errors.Error).ErrorCode() == errors.ErrCodeRegisterPairingNotFound.Code()
}

Do:

1
2
3
4
5
6
7
8
9
10
import (
"errors"
)

func isPairingNotFound(err error) bool {
var serviceErr service.Error
if errors.As(err, &serviceErr) {
return serviceErr.ErrorCode() == errors.ErrCodeRegisterPairingNotFound.Code()
}
}

The Unwrap, Is and As functions work on errors that may wrap other errors. An error wraps another error if its type has the method Unwrap() error
If e.Unwrap() returns a non-nil error w, then we say that e wraps w.