diff --git a/errors/error.go b/errors/error.go index cbc4243..d56ab0e 100644 --- a/errors/error.go +++ b/errors/error.go @@ -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 @@ -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 { diff --git a/errors/error_test.go b/errors/error_test.go index 515dfec..17e4a8a 100644 --- a/errors/error_test.go +++ b/errors/error_test.go @@ -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()) }