Skip to content

Commit

Permalink
Fix repeating error message
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mizutani committed Jan 15, 2023
1 parent 990c887 commit e6f257d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
19 changes: 4 additions & 15 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions test/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit e6f257d

Please sign in to comment.