Skip to content

Commit

Permalink
Merge PR cosmos#5887: types/errors: make Wrap() work like github.com/…
Browse files Browse the repository at this point in the history
…pkg/errors.Wrap
  • Loading branch information
alexanderbez authored Mar 28, 2020
2 parents 77dd247 + 0ba4e3a commit 3c1ce5b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
33 changes: 11 additions & 22 deletions types/errors/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestABCInfo(t *testing.T) {
"wrapped SDK error": {
err: Wrap(Wrap(ErrUnauthorized, "foo"), "bar"),
debug: false,
wantLog: "unauthorized: foo: bar",
wantLog: "bar: foo: unauthorized",
wantCode: ErrUnauthorized.code,
wantSpace: RootCodespace,
},
Expand Down Expand Up @@ -95,15 +95,9 @@ func TestABCInfo(t *testing.T) {
tc := tc
t.Run(testName, func(t *testing.T) {
space, code, log := ABCIInfo(tc.err, tc.debug)
if space != tc.wantSpace {
t.Errorf("want %s space, got %s", tc.wantSpace, space)
}
if code != tc.wantCode {
t.Errorf("want %d code, got %d", tc.wantCode, code)
}
if log != tc.wantLog {
t.Errorf("want %q log, got %q", tc.wantLog, log)
}
require.Equal(t, tc.wantSpace, space)
require.Equal(t, tc.wantCode, code)
require.Equal(t, tc.wantLog, log)
})
}
}
Expand All @@ -119,19 +113,19 @@ func TestABCIInfoStacktrace(t *testing.T) {
err: Wrap(ErrUnauthorized, "wrapped"),
debug: true,
wantStacktrace: true,
wantErrMsg: "unauthorized: wrapped",
wantErrMsg: "wrapped: unauthorized",
},
"wrapped SDK error in non-debug mode does not have stacktrace": {
err: Wrap(ErrUnauthorized, "wrapped"),
debug: false,
wantStacktrace: false,
wantErrMsg: "unauthorized: wrapped",
wantErrMsg: "wrapped: unauthorized",
},
"wrapped stdlib error in debug mode provides stacktrace": {
err: Wrap(fmt.Errorf("stdlib"), "wrapped"),
debug: true,
wantStacktrace: true,
wantErrMsg: "stdlib: wrapped",
wantErrMsg: "wrapped: stdlib",
},
"wrapped stdlib error in non-debug mode does not have stacktrace": {
err: Wrap(fmt.Errorf("stdlib"), "wrapped"),
Expand Down Expand Up @@ -165,10 +159,7 @@ func TestABCIInfoStacktrace(t *testing.T) {
func TestABCIInfoHidesStacktrace(t *testing.T) {
err := Wrap(ErrUnauthorized, "wrapped")
_, _, log := ABCIInfo(err, false)

if log != "unauthorized: wrapped" {
t.Fatalf("unexpected message in non debug mode: %s", log)
}
require.Equal(t, "wrapped: unauthorized", log)
}

func TestRedact(t *testing.T) {
Expand Down Expand Up @@ -208,12 +199,12 @@ func TestABCIInfoSerializeErr(t *testing.T) {
"single error": {
src: myErrDecode,
debug: false,
exp: "tx parse error: test",
exp: "test: tx parse error",
},
"second error": {
src: myErrAddr,
debug: false,
exp: "invalid address: tester",
exp: "tester: invalid address",
},
"single error with debug": {
src: myErrDecode,
Expand Down Expand Up @@ -260,9 +251,7 @@ func TestABCIInfoSerializeErr(t *testing.T) {
spec := spec
t.Run(msg, func(t *testing.T) {
_, _, log := ABCIInfo(spec.src, spec.debug)
if exp, got := spec.exp, log; exp != got {
t.Errorf("expected %v but got %v", exp, got)
}
require.Equal(t, spec.exp, log)
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion types/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ type wrappedError struct {
}

func (e *wrappedError) Error() string {
return fmt.Sprintf("%s: %s", e.parent.Error(), e.msg)
return fmt.Sprintf("%s: %s", e.msg, e.parent.Error())
}

func (e *wrappedError) Cause() error {
Expand Down
25 changes: 25 additions & 0 deletions types/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,28 @@ func TestWrappedUnwrapFail(t *testing.T) {
err := Wrap(errTest2, Wrap(errTest, "some random description").Error())
require.NotEqual(t, errTest, stdlib.Unwrap(err))
}

func TestABCIError(t *testing.T) {
require.Equal(t, "custom: tx parse error", ABCIError(RootCodespace, 2, "custom").Error())
require.Equal(t, "custom: unknown", ABCIError("unknown", 1, "custom").Error())
}

func ExampleWrap() {
err1 := Wrap(ErrInsufficientFunds, "90 is smaller than 100")
err2 := errors.Wrap(ErrInsufficientFunds, "90 is smaller than 100")
fmt.Println(err1.Error())
fmt.Println(err2.Error())
// Output:
// 90 is smaller than 100: insufficient funds
// 90 is smaller than 100: insufficient funds
}

func ExampleWrapf() {
err1 := Wrap(ErrInsufficientFunds, "90 is smaller than 100")
err2 := errors.Wrap(ErrInsufficientFunds, "90 is smaller than 100")
fmt.Println(err1.Error())
fmt.Println(err2.Error())
// Output:
// 90 is smaller than 100: insufficient funds
// 90 is smaller than 100: insufficient funds
}
8 changes: 4 additions & 4 deletions types/errors/stacktrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ func TestStackTrace(t *testing.T) {
}{
"New gives us a stacktrace": {
err: Wrap(ErrNoSignatures, "name"),
wantError: "no signatures supplied: name",
wantError: "name: no signatures supplied",
},
"Wrapping stderr gives us a stacktrace": {
err: Wrap(fmt.Errorf("foo"), "standard"),
wantError: "foo: standard",
wantError: "standard: foo",
},
"Wrapping pkg/errors gives us clean stacktrace": {
err: Wrap(errors.New("bar"), "pkg"),
wantError: "bar: pkg",
wantError: "pkg: bar",
},
"Wrapping inside another function is still clean": {
err: Wrap(fmt.Errorf("indirect"), "do the do"),
wantError: "indirect: do the do",
wantError: "do the do: indirect",
},
}

Expand Down

0 comments on commit 3c1ce5b

Please sign in to comment.