diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index d7f714a..0734c36 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -9,7 +9,7 @@ on: env: SSH_AUTH_SOCK: /tmp/ssh_agent.sock - GOLANGCI_LINT_VERSION: v2.6.2 + GOLANGCI_LINT_VERSION: v2.15.0 jobs: lint: diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 9878ed2..91c262f 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -13,8 +13,8 @@ jobs: strategy: matrix: go-version: - - 1.24.x - - 1.25.x + - 1.26.x + - 1.27.x os: [ ubuntu-latest ] runs-on: ${{ matrix.os }} steps: diff --git a/errors.go b/errors.go index 14f2fc6..d885af8 100644 --- a/errors.go +++ b/errors.go @@ -12,17 +12,70 @@ const NoMsg = "" // Import all the standard errors functions as a convenience. -// Is reports whether any error in err's chain matches target. +// Is reports whether any error in err's tree matches target. +// The target must be comparable. +// +// The tree consists of err itself, followed by the errors obtained by repeatedly +// calling its Unwrap() error or Unwrap() []error method. When err wraps multiple +// errors, Is examines err followed by a depth-first traversal of its children. +// +// 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. +// +// An error type might provide an Is method so it can be treated as equivalent +// to an existing error. For example, if MyError defines +// +// func (m MyError) Is(target error) bool { return target == fs.ErrExist } +// +// then Is(MyError{}, fs.ErrExist) returns true. See [syscall.Errno.Is] for +// an example in the standard library. An Is method should only shallowly +// compare err and the target and not call [Unwrap] on either. func Is(err, target error) bool { return errors.Is(err, target) } -// As finds the first error in err's chain that matches target, and if so, sets -// target to that error value and returns true. +// As finds the first error in err's tree that matches target, and if one is found, sets +// target to that error value and returns true. Otherwise, it returns false. +// +// For most uses, prefer [AsType]. As is equivalent to [AsType] but sets its target +// argument rather than returning the matching error and doesn't require its target +// argument to implement error. +// +// The tree consists of err itself, followed by the errors obtained by repeatedly +// calling its Unwrap() error or Unwrap() []error method. When err wraps multiple +// errors, As examines err followed by a depth-first traversal of its children. +// +// An error matches target if the error's concrete value is assignable to the value +// pointed to by target, or if the error has a method As(any) bool such that +// As(target) returns true. In the latter case, the As method is responsible for +// setting target. +// +// An error type might provide an As method so it can be treated as if it were a +// different error type. +// +// As panics if target is not a non-nil pointer to either a type that implements +// error, or to any interface type. func As(err error, target any) bool { return errors.As(err, target) } +// AsType finds the first error in err's tree that matches the type E, and +// if one is found, returns that error value and true. Otherwise, it +// returns the zero value of E and false. +// +// The tree consists of err itself, followed by the errors obtained by +// repeatedly calling its Unwrap() error or Unwrap() []error method. When +// err wraps multiple errors, AsType examines err followed by a +// depth-first traversal of its children. +// +// An error err matches the type E if the type assertion err.(E) holds, +// or if the error has a method As(any) bool such that err.As(target) +// returns true when target is a non-nil *E. In the latter case, the As +// method is responsible for setting target. +func AsType[E error](err error) (E, bool) { + return errors.AsType[E](err) +} + // New returns an error that formats as the given text. // Each call to New returns a distinct error value even if the text is identical. func New(text string) error { @@ -32,6 +85,9 @@ func New(text string) error { // Unwrap returns the result of calling the Unwrap method on err, if err's // type contains an Unwrap method returning error. // Otherwise, Unwrap returns nil. +// +// Unwrap only calls a method of the form "Unwrap() error". +// In particular Unwrap does not unwrap errors returned by [Join]. func Unwrap(err error) error { return errors.Unwrap(err) } @@ -40,10 +96,12 @@ func Unwrap(err error) error { // value that satisfies error. // // If the format specifier includes a %w verb with an error operand, -// the returned error will implement an Unwrap method returning the operand. It is -// invalid to include more than one %w verb or to supply it with an operand -// that does not implement the error interface. The %w verb is otherwise -// a synonym for %v. +// the returned error will implement an Unwrap method returning the operand. +// If there is more than one %w verb, the returned error will implement an +// Unwrap method returning a []error containing all the %w operands in the +// order they appear in the arguments. +// It is invalid to supply the %w verb with an operand that does not implement +// the error interface. The %w verb is otherwise a synonym for %v. func Errorf(format string, a ...any) error { return fmt.Errorf(format, a...) } @@ -73,7 +131,7 @@ func Last(err error, target any) bool { } val := reflect.ValueOf(target) typ := val.Type() - if typ.Kind() != reflect.Ptr || val.IsNil() { + if typ.Kind() != reflect.Pointer || val.IsNil() { panic("errors: target must be a non-nil pointer") } targetType := typ.Elem() @@ -105,6 +163,7 @@ func Last(err error, target any) bool { // between each string. // // A non-nil error returned by Join implements the Unwrap() []error method. +// The errors may be inspected with [Is] and [As]. func Join(errs ...error) error { return errors.Join(errs...) } diff --git a/errors_test.go b/errors_test.go index 55b8bce..dcb47d7 100644 --- a/errors_test.go +++ b/errors_test.go @@ -30,6 +30,22 @@ func TestAs(t *testing.T) { assert.True(t, errors.As(top, &exp)) } +// Ensure errors.AsType remains stdlib compliant +func TestAsType(t *testing.T) { + err := &ErrTest{Msg: "bottom"} + top := fmt.Errorf("top: %w", err) + + var exp1 *ErrTest + exp1, ok1 := stderr.AsType[*ErrTest](top) + assert.True(t, ok1) + assert.Equal(t, "bottom", exp1.Error()) + + var exp2 *ErrTest + exp2, ok2 := errors.AsType[*ErrTest](top) + assert.True(t, ok2) + assert.Equal(t, "bottom", exp2.Error()) +} + func TestLast(t *testing.T) { err := errors.New("bottom") err = errors.Wrap(err, "last") diff --git a/fields.go b/fields.go index 6a43ebd..61fc42a 100644 --- a/fields.go +++ b/fields.go @@ -194,7 +194,7 @@ func (c *fields) FormatFields() string { if count > 0 { buf.WriteString(", ") } - buf.WriteString(fmt.Sprintf("%+v=%+v", key, value)) + _, _ = fmt.Fprintf(&buf, "%+v=%+v", key, value) count++ } return buf.String() diff --git a/go.mod b/go.mod index 25ddf4e..799106b 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/mailgun/errors -go 1.24 +go 1.26.11 require ( github.com/sirupsen/logrus v1.9.3