Skip to content

Commit

Permalink
[Errors] Add WithError() to Error interface in order to wrap source e…
Browse files Browse the repository at this point in the history
…rror.
  • Loading branch information
jkuma committed Jun 9, 2023
1 parent 968b886 commit 2183079
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Error interface {
WithMessage(format string, args ...interface{}) Error
WithKind(string) Error
WithStatus(int) Error
WithError(error) Error
}

// FullError is a concrete error that implements the Error interface
Expand Down Expand Up @@ -126,6 +127,14 @@ func (e *FullError) WithKind(kind string) Error {
return e
}

// WithError wrap source error.
func (e *FullError) WithError(err error) Error {
e.sourceErr = err
e.errorMessage = err.Error()

return e
}

// HttpError is used to json.Marshal or xml.Marshal FullError.
// You can use it to decode an incoming error.
type HttpError struct {
Expand Down
6 changes: 6 additions & 0 deletions errors/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ func TestErr(t *testing.T) {
assert.Equal(t, expectedKind, err.Kind())
assert.Equal(t, expectedMessage, err.Message())
assert.Equal(t, expectedStatusCode, err.StatusCode())

otherError := errors.New("this is an another error")

err = err.WithError(otherError)

assert.Equal(t, otherError.Error(), err.Error())
}

0 comments on commit 2183079

Please sign in to comment.