diff --git a/CHANGELOG.md b/CHANGELOG.md index a7ec7a7..eddb28c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# v1.0.3 (Jan 7, 2023) + +- Create a function to get error message. + # v1.0.2 (Jan 7, 2023) - Exception.New handles error more exactly. diff --git a/error.go b/error.go index a411347..086726d 100644 --- a/error.go +++ b/error.go @@ -51,3 +51,11 @@ func Or(errs ...error) error { return nil } + +// Message returns only the error message. +func Message(e error) string { + if xe, ok := e.(Error); ok { + return xe.msg + } + return e.Error() +} diff --git a/error_test.go b/error_test.go index 25d6185..a1f17a1 100644 --- a/error_test.go +++ b/error_test.go @@ -21,6 +21,7 @@ package xyerror_test import ( + "errors" "testing" "github.com/xybor-x/xycond" @@ -67,3 +68,11 @@ func TestCombine(t *testing.T) { xycond.ExpectError(err, xyerror.TypeError).Test(t) xycond.ExpectErrorNot(err, xyerror.IndexError).Test(t) } + +func TestMessage(t *testing.T) { + var e1 = xyerror.ValueError.New(t.Name()) + var e2 = errors.New(t.Name()) + xycond.ExpectEqual(e1.Error(), "ValueError: "+t.Name()).Test(t) + xycond.ExpectEqual(xyerror.Message(e1), t.Name()).Test(t) + xycond.ExpectEqual(xyerror.Message(e2), t.Name()).Test(t) +}