diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f9bd79..a7ec7a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# v1.0.2 (Jan 7, 2023) + +- Exception.New handles error more exactly. + # v1.0.1 (Jan 7, 2023) - Exceptions with different parent now can have the same name. diff --git a/exception.go b/exception.go index e25208d..11e0fbc 100644 --- a/exception.go +++ b/exception.go @@ -81,6 +81,12 @@ func (exc Exception) Newf(msg string, a ...any) Error { // New creates an Error with default formatting objects. func (exc Exception) New(a ...any) Error { + for i := range a { + if e, ok := a[i].(Error); ok { + a[i] = e.msg + } + } + return Error{exc: exc, msg: fmt.Sprint(a...)} } diff --git a/exception_test.go b/exception_test.go index 708b7bc..c49fb23 100644 --- a/exception_test.go +++ b/exception_test.go @@ -34,3 +34,14 @@ func TestException(t *testing.T) { xycond.ExpectTrue(strings.Contains(c1.Error(), "class1")).Test(t) xycond.ExpectTrue(strings.Contains(c2.Error(), "class2")).Test(t) } + +func TestExceptionSameName(t *testing.T) { + var c = xyerror.NewException(t.Name()) + xycond.ExpectNotPanic(func() { c.NewException(t.Name()) }).Test(t) +} + +func TestExceptionNewError(t *testing.T) { + var e1 = xyerror.ValueError.New(t.Name()) + var e2 = xyerror.TypeError.New(e1) + xycond.ExpectEqual(e2.Error(), "TypeError: "+t.Name()).Test(t) +}