Compare commits

..

1 Commits
v1.1.1 ... main

Author SHA1 Message Date
6f60e54fd7 add At function 2025-08-11 23:05:16 +02:00
2 changed files with 22 additions and 2 deletions

View File

@ -4,11 +4,13 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"os" "os"
"reflect"
"strings" "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 IsSameAs(err error) bool
} }
@ -30,6 +32,13 @@ 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 { func (ex *custom) IsSameAs(err error) bool {
if err == nil { if err == nil {
return false return false
@ -54,3 +63,14 @@ 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
}

View File

@ -32,9 +32,9 @@ func (suite *CustomExceptionTestSuite) TestShouldAddInnerErrors() {
func (suite *CustomExceptionTestSuite) TestShouldAddInnerErrorsAfterCreation() { func (suite *CustomExceptionTestSuite) TestShouldAddInnerErrorsAfterCreation() {
ex := exception.NewCustom("fail") ex := exception.NewCustom("fail")
ex.With(fmt.Errorf("innerError1"), fmt.Errorf("innerError2")) ex.At(suite).With(fmt.Errorf("innerError1"), fmt.Errorf("innerError2"))
suite.NotNil(ex) suite.NotNil(ex)
suite.Equal(ex.Error(), "[fail]:\ninnerError1\ninnerError2") suite.Equal(ex.Error(), "[fail in *exception_test.CustomExceptionTestSuite]:\ninnerError1\ninnerError2")
} }
func (suite *CustomExceptionTestSuite) TestShouldCompareExceptions() { func (suite *CustomExceptionTestSuite) TestShouldCompareExceptions() {