From 7ac6649da5a2d6077bb3c1a04d772c18e1eb152f Mon Sep 17 00:00:00 2001 From: huykingsofm Date: Sat, 7 Jan 2023 12:53:46 +0000 Subject: [PATCH] Method New handles error more exactly --- CHANGELOG.md | 4 ++++ exception.go | 6 ++++++ exception_test.go | 11 +++++++++++ 3 files changed, 21 insertions(+) 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) +}