Skip to content

Commit

Permalink
fix(api): wrapping a multi error should not reset 'from' infos
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt committed Aug 21, 2020
1 parent d28bb81 commit f0a8825
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
14 changes: 13 additions & 1 deletion sdk/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,10 +808,22 @@ func WrapError(err error, format string, args ...interface{}) error {
}
}

httpError := ErrUnknownError

if e, ok := err.(*MultiError); ok {
var ss []string
for i := range *e {
ss = append(ss, ExtractHTTPError((*e)[i], "").printLight())
}
httpError.From = strings.Join(ss, ", ")
} else {
httpError.From = err.Error()
}

return errorWithStack{
root: errors.Wrap(err, m),
stack: callers(),
httpError: ErrUnknownError,
httpError: httpError,
}
}

Expand Down
17 changes: 11 additions & 6 deletions sdk/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,24 @@ func TestMultiError(t *testing.T) {
mError.Append(errThree)
mError.Append(errFourth)

assert.Equal(t, "TestMultiError: internal server error (caused by: my first error), TestMultiError: wrong request (from: my second error), TestMultiError: internal server error (caused by: hidden info: my third error), TestMultiError: already exists (from: my fourth error)", mError.Error())
assert.Equal(t, "TestMultiError: internal server error (caused by: my first error), TestMultiError: wrong request (from: my second error), TestMultiError: internal server error (from: my third error) (caused by: hidden info: my third error), TestMultiError: already exists (from: my fourth error)", mError.Error())
httpErr := ExtractHTTPError(mError, "fr")
assert.Equal(t, "erreur interne, la requête est incorrecte: my second error, erreur interne, conflit: my fourth error", httpErr.Error())
assert.Equal(t, "erreur interne, la requête est incorrecte: my second error, erreur interne: my third error, conflit: my fourth error", httpErr.Error())

wrappedErr := NewError(ErrWrongRequest, mError)
assert.Equal(t, "TestMultiError: wrong request (from: internal server error, wrong request: my second error, internal server error, already exists: my fourth error) (caused by: TestMultiError: internal server error (caused by: my first error), TestMultiError: wrong request (from: my second error), TestMultiError: internal server error (caused by: hidden info: my third error), TestMultiError: already exists (from: my fourth error))", wrappedErr.Error())
assert.Equal(t, "TestMultiError: wrong request (from: internal server error, wrong request: my second error, internal server error: my third error, already exists: my fourth error) (caused by: TestMultiError: internal server error (caused by: my first error), TestMultiError: wrong request (from: my second error), TestMultiError: internal server error (from: my third error) (caused by: hidden info: my third error), TestMultiError: already exists (from: my fourth error))", wrappedErr.Error())
httpErr = ExtractHTTPError(wrappedErr, "fr")
assert.Equal(t, "la requête est incorrecte (from: internal server error, wrong request: my second error, internal server error, already exists: my fourth error)", httpErr.Error())
assert.Equal(t, "la requête est incorrecte (from: internal server error, wrong request: my second error, internal server error: my third error, already exists: my fourth error)", httpErr.Error())

stackErr := WithStack(mError)
assert.Equal(t, "TestMultiError: internal server error (from: internal server error, wrong request: my second error, internal server error, already exists: my fourth error) (caused by: TestMultiError: internal server error (caused by: my first error), TestMultiError: wrong request (from: my second error), TestMultiError: internal server error (caused by: hidden info: my third error), TestMultiError: already exists (from: my fourth error))", stackErr.Error())
assert.Equal(t, "TestMultiError: internal server error (from: internal server error, wrong request: my second error, internal server error: my third error, already exists: my fourth error) (caused by: TestMultiError: internal server error (caused by: my first error), TestMultiError: wrong request (from: my second error), TestMultiError: internal server error (from: my third error) (caused by: hidden info: my third error), TestMultiError: already exists (from: my fourth error))", stackErr.Error())
httpErr = ExtractHTTPError(stackErr, "fr")
assert.Equal(t, "erreur interne (from: internal server error, wrong request: my second error, internal server error, already exists: my fourth error)", httpErr.Error())
assert.Equal(t, "erreur interne (from: internal server error, wrong request: my second error, internal server error: my third error, already exists: my fourth error)", httpErr.Error())

stackErr = WrapError(mError, "more info")
assert.Equal(t, "TestMultiError: internal server error (from: internal server error, wrong request: my second error, internal server error: my third error, already exists: my fourth error) (caused by: more info: TestMultiError: internal server error (caused by: my first error), TestMultiError: wrong request (from: my second error), TestMultiError: internal server error (from: my third error) (caused by: hidden info: my third error), TestMultiError: already exists (from: my fourth error))", stackErr.Error())
httpErr = ExtractHTTPError(stackErr, "fr")
assert.Equal(t, "erreur interne (from: internal server error, wrong request: my second error, internal server error: my third error, already exists: my fourth error)", httpErr.Error())
}

func Test_cause(t *testing.T) {
Expand Down

0 comments on commit f0a8825

Please sign in to comment.