Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

Commit

Permalink
Method New handles error more exactly
Browse files Browse the repository at this point in the history
  • Loading branch information
huykingsofm committed Jan 7, 2023
1 parent 08c833c commit 7ac6649
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 6 additions & 0 deletions exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)}
}

Expand Down
11 changes: 11 additions & 0 deletions exception_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 7ac6649

Please sign in to comment.