Skip to content
Open
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
17 changes: 16 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,24 @@ func New(message string) error {
// Errorf formats according to a format specifier and returns the string
// as a value that satisfies error.
// Errorf also records the stack trace at the point it was called.
// If the format specifier includes a %w verb with an error operand,
// the returned error wraps the result of fmt.Errorf with a stack trace.
func Errorf(format string, args ...interface{}) error {
err := fmt.Errorf(format, args...)
switch u := err.(type) {
case interface{ Unwrap() error }:
if u.Unwrap() != nil {
return &withStack{err, callers()}
}
case interface{ Unwrap() []error }:
for _, unwrapped := range u.Unwrap() {
if unwrapped != nil {
return &withStack{err, callers()}
}
}
}
return &fundamental{
msg: fmt.Sprintf(format, args...),
msg: err.Error(),
stack: callers(),
}
}
Expand Down
113 changes: 113 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"reflect"
"strings"
"testing"
)

Expand Down Expand Up @@ -139,6 +140,10 @@ func TestErrorf(t *testing.T) {
}{
{Errorf("read error without format specifiers"), "read error without format specifiers"},
{Errorf("read error with %d format specifier", 1), "read error with 1 format specifier"},
{Errorf("read error: %w", io.EOF), "read error: EOF"},
{Errorf("%w: client error", io.EOF), "EOF: client error"},
{Errorf("read %d bytes error: %w", 42, io.EOF), "read 42 bytes error: EOF"},
{Errorf("%w", io.EOF), "EOF"},
}

for _, tt := range tests {
Expand All @@ -149,6 +154,114 @@ func TestErrorf(t *testing.T) {
}
}

func TestErrorfWrapping(t *testing.T) {
base := errors.New("base error")
wrapped := Errorf("wrapped: %w", base)

// Verify Unwrap() chain reaches base via stdlib errors.Is
u, ok := wrapped.(interface{ Unwrap() error })
if !ok {
t.Fatal("expected wrapped error to implement Unwrap() error")
}
if !errors.Is(wrapped, base) {
t.Errorf("errors.Is(wrapped, base) = false, want true")
}
if got := errors.Unwrap(u.Unwrap()); got != base {
t.Errorf("Unwrap(Unwrap(wrapped)) = %v, want %v", got, base)
}

// Verify nested Unwrap chains
outer := Errorf("outer: %w", wrapped)
if !errors.Is(outer, wrapped) {
t.Errorf("errors.Is(outer, wrapped) = false, want true")
}
if !errors.Is(outer, base) {
t.Errorf("errors.Is(outer, base) = false, want true")
}

// Verify non-%w returns *fundamental without Unwrap()
plain := Errorf("plain %s", "error")
if _, ok := plain.(*fundamental); !ok {
t.Errorf("plain type = %T, want *fundamental", plain)
}
if _, ok := plain.(interface{ Unwrap() error }); ok {
t.Errorf("Errorf without %%w should not implement Unwrap() error")
}

// Verify %w with nil error operand returns *fundamental without Unwrap()
var nilErr error
wrapNil := Errorf("wrap nil: %w", nilErr)
if _, ok := wrapNil.(*fundamental); !ok {
t.Errorf("wrapNil type = %T, want *fundamental", wrapNil)
}
if _, ok := wrapNil.(interface{ Unwrap() error }); ok {
t.Errorf("wrapNil should not implement Unwrap() error")
}
if got := Cause(wrapNil); got != wrapNil {
t.Errorf("Cause(wrapNil) = %v, want %v", got, wrapNil)
}

// Verify StackTrace
st, ok := wrapped.(interface{ StackTrace() StackTrace })
if !ok {
t.Fatal("expected wrapped error to implement StackTrace()")
}
if len(st.StackTrace()) == 0 {
t.Error("expected non-empty stack trace")
}

// Verify formatting verbs
if got := fmt.Sprintf("%s", wrapped); got != "wrapped: base error" {
t.Errorf("%%s format = %q, want %q", got, "wrapped: base error")
}
if got := fmt.Sprintf("%v", wrapped); got != "wrapped: base error" {
t.Errorf("%%v format = %q, want %q", got, "wrapped: base error")
}
if got := fmt.Sprintf("%q", wrapped); got != `"wrapped: base error"` {
t.Errorf("%%q format = %q, want %q", got, `"wrapped: base error"`)
}
plusV := fmt.Sprintf("%+v", wrapped)
if !strings.Contains(plusV, "wrapped: base error") || !strings.Contains(plusV, "TestErrorfWrapping") {
t.Errorf("%%+v format missing expected message or stack: %q", plusV)
}
}

func TestErrorfMultiWrap(t *testing.T) {
err1 := errors.New("err1")
err2 := errors.New("err2")
multi := Errorf("first: %w, second: %w", err1, err2)

if !errors.Is(multi, err1) {
t.Errorf("errors.Is(multi, err1) = false, want true")
}
if !errors.Is(multi, err2) {
t.Errorf("errors.Is(multi, err2) = false, want true")
}
if got := multi.Error(); got != "first: err1, second: err2" {
t.Errorf("multi.Error() = %q, want %q", got, "first: err1, second: err2")
}
if st, ok := multi.(interface{ StackTrace() StackTrace }); !ok || len(st.StackTrace()) == 0 {
t.Errorf("expected multi-wrapped error to have non-empty StackTrace")
}
if got := fmt.Sprintf("%q", multi); got != `"first: err1, second: err2"` {
t.Errorf("%%q format = %q, want %q", got, `"first: err1, second: err2"`)
}
plusV := fmt.Sprintf("%+v", multi)
if !strings.Contains(plusV, "first: err1, second: err2") || !strings.Contains(plusV, "TestErrorfMultiWrap") {
t.Errorf("%%+v format missing expected message or stack: %q", plusV)
}

// Verify all-nil %w operands return *fundamental without Unwrap()
var nilErr error
zeroFromMulti := Errorf("first: %w, second: %w", nilErr, nilErr)
if _, ok := zeroFromMulti.(*fundamental); !ok {
t.Errorf("zeroFromMulti type = %T, want *fundamental", zeroFromMulti)
}
if _, ok := zeroFromMulti.(interface{ Unwrap() error }); ok {
t.Errorf("zeroFromMulti should not implement Unwrap() error")
}
}

func TestWithStackNil(t *testing.T) {
got := WithStack(nil)
if got != nil {
Expand Down
21 changes: 21 additions & 0 deletions go113_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ func TestIs(t *testing.T) {
},
want: true,
},
{
name: "errorf with %w",
args: args{
err: Errorf("wrap it: %w", err),
target: err,
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -121,6 +129,14 @@ func TestAs(t *testing.T) {
},
want: true,
},
{
name: "errorf with %w",
args: args{
err: Errorf("wrap it: %w", err),
target: new(customErr),
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -167,6 +183,11 @@ func TestUnwrap(t *testing.T) {
args: args{err: fmt.Errorf("wrap: %w", err)},
want: err,
},
{
name: "errorf with %w",
args: args{err: Unwrap(Errorf("wrap: %w", err))},
want: err,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down