Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
75 changes: 67 additions & 8 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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...)
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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...)
}
Expand Down
16 changes: 16 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/mailgun/errors

go 1.24
go 1.26.11

require (
github.com/sirupsen/logrus v1.9.3
Expand Down
Loading