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

Commit

Permalink
Message method returns a capitalized string
Browse files Browse the repository at this point in the history
  • Loading branch information
huykingsofm committed Jan 7, 2023
1 parent 4df66d3 commit 8fab9f8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 7 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.4 (Jan 7, 2023)

- Capitalize the result of Message function.

# v1.0.3 (Jan 7, 2023)

- Create a function to get error message.
Expand Down
12 changes: 10 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ package xyerror
import (
"errors"
"fmt"

"golang.org/x/text/cases"
"golang.org/x/text/language"
)

// Error is a special error belongs to a generic error class.
Expand Down Expand Up @@ -52,10 +55,15 @@ func Or(errs ...error) error {
return nil
}

var titler = cases.Title(language.English)

// Message returns only the error message.
func Message(e error) string {
var msg = e.Error()
if xe, ok := e.(Error); ok {
return xe.msg
msg = xe.msg
}
return e.Error()

titler.Reset()
return titler.String(msg)
}
10 changes: 5 additions & 5 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func TestCombine(t *testing.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)
var e1 = xyerror.ValueError.New("foo-bar")
var e2 = errors.New("foo-foo")
xycond.ExpectEqual(e1.Error(), "ValueError: foo-bar").Test(t)
xycond.ExpectEqual(xyerror.Message(e1), "Foo-Bar").Test(t)
xycond.ExpectEqual(xyerror.Message(e2), "Foo-Foo").Test(t)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.18
require (
github.com/xybor-x/xycond v0.0.1
github.com/xybor-x/xylock v0.0.1
golang.org/x/text v0.6.0
)

require golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ github.com/xybor-x/xylock v0.0.1 h1:/LhbJb6WJkUW+xE4/bBpIC76BKrbgnayiId6LdouqUw=
github.com/xybor-x/xylock v0.0.1/go.mod h1:IQvixGhBrgKdh0HcCJAqzYzk482SGyYOzezBxK6UdRc=
golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde h1:ejfdSekXMDxDLbRrJMwUk6KnSLZ2McaUCVcIKM+N6jc=
golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.6.0 h1:3XmdazWV+ubf7QgHSTWeykHOci5oeekaGJBLkrkaw4k=
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=

0 comments on commit 8fab9f8

Please sign in to comment.