Compare commits
No commits in common. "main" and "v1.0.0" have entirely different histories.
29
exception.go
29
exception.go
@ -4,15 +4,11 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Custom interface {
|
type Custom interface {
|
||||||
error
|
error
|
||||||
At(source any) Custom
|
|
||||||
With(innerErr ...error) Custom
|
With(innerErr ...error) Custom
|
||||||
IsSameAs(err error) bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCustom(message string, innerErr ...error) Custom {
|
func NewCustom(message string, innerErr ...error) Custom {
|
||||||
@ -32,20 +28,6 @@ func (ex *custom) With(innerErr ...error) Custom {
|
|||||||
return ex
|
return ex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ex *custom) At(source any) Custom {
|
|
||||||
if source != nil {
|
|
||||||
ex.message = fmt.Sprintf("%s in %s", ex.message, getType(source))
|
|
||||||
}
|
|
||||||
return ex
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ex *custom) IsSameAs(err error) bool {
|
|
||||||
if err == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return strings.HasPrefix(err.Error(), fmt.Sprintf("[%s]:", ex.message))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ex *custom) Error() string {
|
func (ex *custom) Error() string {
|
||||||
buf := bytes.NewBufferString("")
|
buf := bytes.NewBufferString("")
|
||||||
if len(ex.innerErrors) > 0 {
|
if len(ex.innerErrors) > 0 {
|
||||||
@ -63,14 +45,3 @@ func getOsNewLine() string {
|
|||||||
}
|
}
|
||||||
return "\r\n"
|
return "\r\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
func getType[T any](source T) string {
|
|
||||||
typeName := ""
|
|
||||||
typeOf := reflect.TypeOf(source)
|
|
||||||
if typeOf != nil {
|
|
||||||
typeName = typeOf.String()
|
|
||||||
} else {
|
|
||||||
typeName = reflect.TypeOf((*T)(nil)).Elem().String()
|
|
||||||
}
|
|
||||||
return typeName
|
|
||||||
}
|
|
||||||
|
|||||||
@ -32,21 +32,9 @@ func (suite *CustomExceptionTestSuite) TestShouldAddInnerErrors() {
|
|||||||
|
|
||||||
func (suite *CustomExceptionTestSuite) TestShouldAddInnerErrorsAfterCreation() {
|
func (suite *CustomExceptionTestSuite) TestShouldAddInnerErrorsAfterCreation() {
|
||||||
ex := exception.NewCustom("fail")
|
ex := exception.NewCustom("fail")
|
||||||
ex.At(suite).With(fmt.Errorf("innerError1"), fmt.Errorf("innerError2"))
|
ex.With(fmt.Errorf("innerError1"), fmt.Errorf("innerError2"))
|
||||||
suite.NotNil(ex)
|
suite.NotNil(ex)
|
||||||
suite.Equal(ex.Error(), "[fail in *exception_test.CustomExceptionTestSuite]:\ninnerError1\ninnerError2")
|
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))
|
|
||||||
suite.False(InvalidArgumentException.IsSameAs(nil))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCustomExceptionTestSuite(t *testing.T) {
|
func TestCustomExceptionTestSuite(t *testing.T) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user