-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding new error response object (#240)
- Loading branch information
1 parent
196a321
commit 42ee002
Showing
4 changed files
with
174 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package error | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type ( | ||
Type string | ||
) | ||
|
||
const ( | ||
ApplicationError Type = "ApplicationError" | ||
CriticalError Type = "CriticalError" | ||
UnknownError Type = "UnknownError" | ||
) | ||
|
||
type Response struct { | ||
Valid bool | ||
Err error | ||
ErrorType Type | ||
} | ||
|
||
func (r *Response) Error() string { | ||
return fmt.Sprintf("valid %v: err %v, error type: %s", r.Valid, r.Err, r.ErrorType) | ||
} | ||
|
||
func NewErrorResponse(errorType Type, errorMessage string) *Response { | ||
return &Response{ | ||
Valid: isValid(errorType), | ||
ErrorType: errorType, | ||
Err: errors.New(errorMessage), | ||
} | ||
} | ||
|
||
func NewErrorResponsef(errorType Type, format string, a ...interface{}) *Response { | ||
return &Response{ | ||
Valid: false, | ||
ErrorType: ApplicationError, | ||
Err: errors.Errorf(format, a...), | ||
} | ||
} | ||
|
||
func NewErrorResponseWithError(errorType Type, err error) *Response { | ||
return &Response{ | ||
Valid: isValid(errorType), | ||
ErrorType: errorType, | ||
Err: err, | ||
} | ||
} | ||
|
||
// GetErrorResponse will get the type of err, if the type is a ErrorResponse it will return that as an ErrorResponse, otherwise it will construct a new default ErrorResponse | ||
func GetErrorResponse(err error) Response { | ||
var errRes *Response | ||
|
||
if errors.As(err, &errRes) { | ||
return *errRes | ||
} | ||
|
||
return Response{Valid: false, Err: err, ErrorType: UnknownError} | ||
} | ||
|
||
func isValid(errorType Type) bool { | ||
if errorType == ApplicationError { | ||
return true | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package error | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestErrorResponse(t *testing.T) { | ||
t.Run("simple error", func(tt *testing.T) { | ||
err := standardErr() | ||
resp := GetErrorResponse(err) | ||
|
||
assert.Equal(tt, resp.Valid, false) | ||
assert.Equal(tt, resp.ErrorType, UnknownError) | ||
assert.Equal(tt, resp.Err, err) | ||
}) | ||
|
||
t.Run("error response with string", func(tt *testing.T) { | ||
err := errResponse() | ||
resp := GetErrorResponse(err) | ||
|
||
assert.Equal(tt, resp.Valid, true) | ||
assert.Equal(tt, resp.ErrorType, ApplicationError) | ||
assert.Equal(tt, resp.Err, err.(*Response).Err) | ||
}) | ||
|
||
t.Run("error response with string", func(tt *testing.T) { | ||
err := errResponseWithErr() | ||
resp := GetErrorResponse(err) | ||
|
||
assert.Equal(tt, resp.Valid, false) | ||
assert.Equal(tt, resp.ErrorType, CriticalError) | ||
assert.Equal(tt, resp.Err, err.(*Response).Err) | ||
}) | ||
|
||
t.Run("error response with formatted string", func(tt *testing.T) { | ||
err := errResponseWithFormattedMsg() | ||
resp := GetErrorResponse(err) | ||
|
||
assert.Equal(tt, resp.Valid, false) | ||
assert.Equal(tt, resp.ErrorType, ApplicationError) | ||
assert.Equal(tt, resp.Err, err.(*Response).Err) | ||
assert.Contains(tt, resp.Error(), "the best number: 5") | ||
}) | ||
} | ||
|
||
func standardErr() error { | ||
return errors.New("this is a normal error") | ||
} | ||
|
||
func errResponse() error { | ||
return NewErrorResponse(ApplicationError, "this is error response with message") | ||
} | ||
|
||
func errResponseWithErr() error { | ||
err := errors.New("this is error response with error") | ||
return NewErrorResponseWithError(CriticalError, err) | ||
} | ||
|
||
func errResponseWithFormattedMsg() error { | ||
return NewErrorResponsef(CriticalError, "check out this error and the best number: %d", 5) | ||
} |