Skip to content

Commit

Permalink
feat: add fallback for Errorf
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Nov 5, 2021
1 parent 0633115 commit da2567e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion format.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package errors

import (
"fmt"
"strings"
)

// A Formatter formats error messages.
Expand Down Expand Up @@ -39,7 +40,7 @@ type Printer interface {

// Errorf creates new error with format.
func Errorf(format string, a ...interface{}) error {
if !Trace() {
if !Trace() || strings.Contains(format, "%w") {
return fmt.Errorf(format, a...)
}
return &errorString{fmt.Sprintf(format, a...), Caller(1)}
Expand Down
6 changes: 6 additions & 0 deletions format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,9 @@ func (e fmtTwiceErr) GoString() string {
type panicValue struct{}

func (panicValue) String() string { panic("panic") }

func TestFormatError(t *testing.T) {
if !errors.Is(errors.Errorf("foo: %w", io.EOF), io.EOF) {
t.Error("fallback expected")
}
}

0 comments on commit da2567e

Please sign in to comment.