Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit f85d45f

Browse files
committed
Merge pull request #7 from umairidris/wrapf
Add Wrapf
2 parents c86dfce + 9a17912 commit f85d45f

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

errors.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,20 @@ func Wrap(cause error, message string) error {
147147
}
148148
}
149149

150+
// Wrapf returns an error annotating the cause with the format specifier.
151+
// If cause is nil, Wrapf returns nil.
152+
func Wrapf(cause error, format string, args ...interface{}) error {
153+
if cause == nil {
154+
return nil
155+
}
156+
pc, _, _, _ := runtime.Caller(1)
157+
return &e{
158+
cause: cause,
159+
message: fmt.Sprintf(format, args...),
160+
loc: loc(pc),
161+
}
162+
}
163+
150164
type causer interface {
151165
Cause() error
152166
}

errors_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ func TestFprint(t *testing.T) {
130130
}, {
131131
err: Wrap(Wrap(x, "message"), "another message"),
132132
want: "github.com/pkg/errors/errors_test.go:131: another message\ngithub.com/pkg/errors/errors_test.go:131: message\ngithub.com/pkg/errors/errors_test.go:106: error\n",
133+
}, {
134+
err: Wrapf(x, "message"),
135+
want: "github.com/pkg/errors/errors_test.go:134: message\ngithub.com/pkg/errors/errors_test.go:106: error\n",
133136
}}
134137

135138
for i, tt := range tests {
@@ -141,3 +144,29 @@ func TestFprint(t *testing.T) {
141144
}
142145
}
143146
}
147+
148+
func TestWrapfNil(t *testing.T) {
149+
got := Wrapf(nil, "no error")
150+
if got != nil {
151+
t.Errorf("Wrapf(nil, \"no error\"): got %#v, expected nil", got)
152+
}
153+
}
154+
155+
func TestWrapf(t *testing.T) {
156+
tests := []struct {
157+
err error
158+
message string
159+
want string
160+
}{
161+
{io.EOF, "read error", "read error: EOF"},
162+
{Wrapf(io.EOF, "read error without format specifiers"), "client error", "client error: read error without format specifiers: EOF"},
163+
{Wrapf(io.EOF, "read error with %d format specifier", 1), "client error", "client error: read error with 1 format specifier: EOF"},
164+
}
165+
166+
for _, tt := range tests {
167+
got := Wrapf(tt.err, tt.message).Error()
168+
if got != tt.want {
169+
t.Errorf("Wrapf(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)
170+
}
171+
}
172+
}

example_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,11 @@ func ExampleFprint() {
5454
// github.com/pkg/errors/example_test.go:34: inner
5555
// github.com/pkg/errors/example_test.go:33: error
5656
}
57+
58+
func ExampleWrapf() {
59+
cause := errors.New("whoops")
60+
err := errors.Wrapf(cause, "oh noes #%d", 2)
61+
fmt.Println(err)
62+
63+
// Output: oh noes #2: whoops
64+
}

0 commit comments

Comments
 (0)