Skip to content

Commit

Permalink
Added errors.Errorf() (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
thrawn01 authored Apr 17, 2023
1 parent b568da4 commit 97c51d8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
13 changes: 13 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package errors

import (
"errors"
"fmt"
"reflect"
)

Expand Down Expand Up @@ -35,6 +36,18 @@ func Unwrap(err error) error {
return errors.Unwrap(err)
}

// Errorf formats according to a format specifier and returns the string as a
// 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.
func Errorf(format string, a ...any) error {
return fmt.Errorf(format, a...)
}

// Last finds the last error in err's chain that matches target, and if one is found, sets
// target to that error value and returns true. Otherwise, it returns false.
//
Expand Down
3 changes: 1 addition & 2 deletions errors_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package errors_test

import (
"fmt"
"testing"

"github.com/mailgun/errors"
Expand Down Expand Up @@ -45,7 +44,7 @@ func TestLast(t *testing.T) {
err = errors.Wrap(err, "last")
err = errors.Wrap(err, "second")
err = errors.Wrap(err, "first")
err = fmt.Errorf("wrapped: %w", err)
err = errors.Errorf("wrapped: %w", err)

// errors.As() returns the "first" error in the chain with a stack trace
var first callstack.HasStackTrace
Expand Down

0 comments on commit 97c51d8

Please sign in to comment.