add IsSameAs method
This commit is contained in:
parent
1f211de8ef
commit
e9716a72e6
@ -4,11 +4,13 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Custom interface {
|
||||
error
|
||||
With(innerErr ...error) Custom
|
||||
IsSameAs(err error) bool
|
||||
}
|
||||
|
||||
func NewCustom(message string, innerErr ...error) Custom {
|
||||
@ -28,6 +30,10 @@ func (ex *custom) With(innerErr ...error) Custom {
|
||||
return ex
|
||||
}
|
||||
|
||||
func (ex *custom) IsSameAs(err error) bool {
|
||||
return strings.HasPrefix(err.Error(), fmt.Sprintf("[%s]:", ex.message))
|
||||
}
|
||||
|
||||
func (ex *custom) Error() string {
|
||||
buf := bytes.NewBufferString("")
|
||||
if len(ex.innerErrors) > 0 {
|
||||
|
||||
@ -37,6 +37,17 @@ func (suite *CustomExceptionTestSuite) TestShouldAddInnerErrorsAfterCreation() {
|
||||
suite.Equal(ex.Error(), "[fail]:\ninnerError1\ninnerError2")
|
||||
}
|
||||
|
||||
func (suite *CustomExceptionTestSuite) TestShouldCompareExceptions() {
|
||||
InvalidArgumentException := exception.NewCustom("InvalidArgumentException")
|
||||
NotFoundException := exception.NewCustom("NotFoundException")
|
||||
customErr := fmt.Errorf("something")
|
||||
|
||||
suite.True(InvalidArgumentException.IsSameAs(InvalidArgumentException))
|
||||
suite.False(NotFoundException.IsSameAs(InvalidArgumentException))
|
||||
suite.False(NotFoundException.IsSameAs(customErr))
|
||||
suite.False(InvalidArgumentException.IsSameAs(customErr))
|
||||
}
|
||||
|
||||
func TestCustomExceptionTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(CustomExceptionTestSuite))
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user