From e9716a72e677113ea105eca27fb63022fae1f407 Mon Sep 17 00:00:00 2001 From: admin Date: Thu, 7 Aug 2025 15:50:06 +0200 Subject: [PATCH] add IsSameAs method --- exception.go | 6 ++++++ exception_test.go | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/exception.go b/exception.go index 51ea232..e4aacf4 100644 --- a/exception.go +++ b/exception.go @@ -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 { diff --git a/exception_test.go b/exception_test.go index 7a975c6..eae5ae5 100644 --- a/exception_test.go +++ b/exception_test.go @@ -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)) }