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

Commit

Permalink
Create a function to get error message
Browse files Browse the repository at this point in the history
  • Loading branch information
huykingsofm committed Jan 7, 2023
1 parent 7ac6649 commit 4df66d3
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.3 (Jan 7, 2023)

- Create a function to get error message.

# v1.0.2 (Jan 7, 2023)

- Exception.New handles error more exactly.
Expand Down
8 changes: 8 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
9 changes: 9 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package xyerror_test

import (
"errors"
"testing"

"github.com/xybor-x/xycond"
Expand Down Expand Up @@ -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)
}

0 comments on commit 4df66d3

Please sign in to comment.