From e6f257d49cd5d568a850b91e2a5b8f462b6b5085 Mon Sep 17 00:00:00 2001 From: Masayoshi Mizutani Date: Sun, 15 Jan 2023 11:16:04 +0900 Subject: [PATCH] Fix repeating error message --- errors.go | 19 ++++--------------- test/errors_test.go | 5 +++++ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/errors.go b/errors.go index 815c326..21e542c 100644 --- a/errors.go +++ b/errors.go @@ -96,24 +96,13 @@ type printable struct { func (x *Error) Error() string { s := x.msg cause := x.cause - for i := 0; i < 16; i++ { - if cause == nil { - break - } - - s = fmt.Sprintf("%s: %v", s, cause.Error()) - type errorUnwrap interface { - Unwrap() error - } - unwrapable, ok := cause.(errorUnwrap) - if !ok { - break - } - - cause = unwrapable.Unwrap() + if cause == nil { + return s } + s = fmt.Sprintf("%s: %v", s, cause.Error()) + return s } diff --git a/test/errors_test.go b/test/errors_test.go index f08aca0..40882bd 100644 --- a/test/errors_test.go +++ b/test/errors_test.go @@ -96,3 +96,8 @@ func TestFormat(t *testing.T) { err := goerr.New("test: %s", "blue") assert.Equal(t, "test: blue", err.Error()) } + +func TestErrorString(t *testing.T) { + err := goerr.Wrap(goerr.Wrap(goerr.New("blue"), "orange"), "red") + assert.Equal(t, "red: orange: blue", err.Error()) +}