Merge PR #5887: types/errors: make Wrap() work like github.com/pkg/errors.Wrap

This commit is contained in:
Alexander Bezobchuk 2020-03-28 12:37:57 -04:00 committed by GitHub
commit 3c1ce5bc41
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 27 deletions

View File

@ -28,7 +28,7 @@ func TestABCInfo(t *testing.T) {
"wrapped SDK error": { "wrapped SDK error": {
err: Wrap(Wrap(ErrUnauthorized, "foo"), "bar"), err: Wrap(Wrap(ErrUnauthorized, "foo"), "bar"),
debug: false, debug: false,
wantLog: "unauthorized: foo: bar", wantLog: "bar: foo: unauthorized",
wantCode: ErrUnauthorized.code, wantCode: ErrUnauthorized.code,
wantSpace: RootCodespace, wantSpace: RootCodespace,
}, },
@ -95,15 +95,9 @@ func TestABCInfo(t *testing.T) {
tc := tc tc := tc
t.Run(testName, func(t *testing.T) { t.Run(testName, func(t *testing.T) {
space, code, log := ABCIInfo(tc.err, tc.debug) space, code, log := ABCIInfo(tc.err, tc.debug)
if space != tc.wantSpace { require.Equal(t, tc.wantSpace, space)
t.Errorf("want %s space, got %s", tc.wantSpace, space) require.Equal(t, tc.wantCode, code)
} require.Equal(t, tc.wantLog, log)
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)
}
}) })
} }
} }
@ -119,19 +113,19 @@ func TestABCIInfoStacktrace(t *testing.T) {
err: Wrap(ErrUnauthorized, "wrapped"), err: Wrap(ErrUnauthorized, "wrapped"),
debug: true, debug: true,
wantStacktrace: true, wantStacktrace: true,
wantErrMsg: "unauthorized: wrapped", wantErrMsg: "wrapped: unauthorized",
}, },
"wrapped SDK error in non-debug mode does not have stacktrace": { "wrapped SDK error in non-debug mode does not have stacktrace": {
err: Wrap(ErrUnauthorized, "wrapped"), err: Wrap(ErrUnauthorized, "wrapped"),
debug: false, debug: false,
wantStacktrace: false, wantStacktrace: false,
wantErrMsg: "unauthorized: wrapped", wantErrMsg: "wrapped: unauthorized",
}, },
"wrapped stdlib error in debug mode provides stacktrace": { "wrapped stdlib error in debug mode provides stacktrace": {
err: Wrap(fmt.Errorf("stdlib"), "wrapped"), err: Wrap(fmt.Errorf("stdlib"), "wrapped"),
debug: true, debug: true,
wantStacktrace: true, wantStacktrace: true,
wantErrMsg: "stdlib: wrapped", wantErrMsg: "wrapped: stdlib",
}, },
"wrapped stdlib error in non-debug mode does not have stacktrace": { "wrapped stdlib error in non-debug mode does not have stacktrace": {
err: Wrap(fmt.Errorf("stdlib"), "wrapped"), err: Wrap(fmt.Errorf("stdlib"), "wrapped"),
@ -165,10 +159,7 @@ func TestABCIInfoStacktrace(t *testing.T) {
func TestABCIInfoHidesStacktrace(t *testing.T) { func TestABCIInfoHidesStacktrace(t *testing.T) {
err := Wrap(ErrUnauthorized, "wrapped") err := Wrap(ErrUnauthorized, "wrapped")
_, _, log := ABCIInfo(err, false) _, _, log := ABCIInfo(err, false)
require.Equal(t, "wrapped: unauthorized", log)
if log != "unauthorized: wrapped" {
t.Fatalf("unexpected message in non debug mode: %s", log)
}
} }
func TestRedact(t *testing.T) { func TestRedact(t *testing.T) {
@ -208,12 +199,12 @@ func TestABCIInfoSerializeErr(t *testing.T) {
"single error": { "single error": {
src: myErrDecode, src: myErrDecode,
debug: false, debug: false,
exp: "tx parse error: test", exp: "test: tx parse error",
}, },
"second error": { "second error": {
src: myErrAddr, src: myErrAddr,
debug: false, debug: false,
exp: "invalid address: tester", exp: "tester: invalid address",
}, },
"single error with debug": { "single error with debug": {
src: myErrDecode, src: myErrDecode,
@ -260,9 +251,7 @@ func TestABCIInfoSerializeErr(t *testing.T) {
spec := spec spec := spec
t.Run(msg, func(t *testing.T) { t.Run(msg, func(t *testing.T) {
_, _, log := ABCIInfo(spec.src, spec.debug) _, _, log := ABCIInfo(spec.src, spec.debug)
if exp, got := spec.exp, log; exp != got { require.Equal(t, spec.exp, log)
t.Errorf("expected %v but got %v", exp, got)
}
}) })
} }
} }

View File

@ -262,7 +262,7 @@ type wrappedError struct {
} }
func (e *wrappedError) Error() string { 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 { func (e *wrappedError) Cause() error {

View File

@ -208,3 +208,28 @@ func TestWrappedUnwrapFail(t *testing.T) {
err := Wrap(errTest2, Wrap(errTest, "some random description").Error()) err := Wrap(errTest2, Wrap(errTest, "some random description").Error())
require.NotEqual(t, errTest, stdlib.Unwrap(err)) 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
}

View File

@ -15,19 +15,19 @@ func TestStackTrace(t *testing.T) {
}{ }{
"New gives us a stacktrace": { "New gives us a stacktrace": {
err: Wrap(ErrNoSignatures, "name"), err: Wrap(ErrNoSignatures, "name"),
wantError: "no signatures supplied: name", wantError: "name: no signatures supplied",
}, },
"Wrapping stderr gives us a stacktrace": { "Wrapping stderr gives us a stacktrace": {
err: Wrap(fmt.Errorf("foo"), "standard"), err: Wrap(fmt.Errorf("foo"), "standard"),
wantError: "foo: standard", wantError: "standard: foo",
}, },
"Wrapping pkg/errors gives us clean stacktrace": { "Wrapping pkg/errors gives us clean stacktrace": {
err: Wrap(errors.New("bar"), "pkg"), err: Wrap(errors.New("bar"), "pkg"),
wantError: "bar: pkg", wantError: "pkg: bar",
}, },
"Wrapping inside another function is still clean": { "Wrapping inside another function is still clean": {
err: Wrap(fmt.Errorf("indirect"), "do the do"), err: Wrap(fmt.Errorf("indirect"), "do the do"),
wantError: "indirect: do the do", wantError: "do the do: indirect",
}, },
} }