Skip to content

Commit

Permalink
✨ feat(errorx): add new util func Err(), Errf() for quick create go e…
Browse files Browse the repository at this point in the history
…rror
  • Loading branch information
inhere committed Apr 27, 2023
1 parent e8fd839 commit fb2a7c2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
11 changes: 10 additions & 1 deletion errorx/errorx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ func TestNew(t *testing.T) {
// fmt.Printf("%#v\n", err)
}

func TestRawGoErr(t *testing.T) {
assert.Err(t, errorx.E("error message"))
assert.Err(t, errorx.Err("error message"))
assert.Err(t, errorx.Raw("error message"))
assert.Err(t, errorx.Ef("error %s", "message"))
assert.Err(t, errorx.Errf("error %s", "message"))
assert.Err(t, errorx.Rawf("error %s", "message"))
}

func TestNewf(t *testing.T) {
err := errorx.Newf("error %s", "message")
assert.Err(t, err)
Expand Down Expand Up @@ -129,7 +138,7 @@ func TestWithPrev_errorx_l2(t *testing.T) {
func TestStacked_goerr(t *testing.T) {
assert.Nil(t, errorx.Stacked(nil))

err1 := errorx.Raw("first error message")
err1 := errorx.E("first error message")
assert.Err(t, err1)

err2 := errorx.Stacked(err1)
Expand Down
22 changes: 21 additions & 1 deletion errorx/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,31 @@ import (
"fmt"
)

// E new a raw go error. alias of errors.New()
func E(msg string) error {
return errors.New(msg)
}

// Err new a raw go error. alias of errors.New()
func Err(msg string) error {
return errors.New(msg)
}

// Raw new a raw go error. alias of errors.New()
func Raw(msg string) error {
return errors.New(msg)
}

// Ef new a raw go error. alias of errors.New()
func Ef(tpl string, vars ...any) error {
return fmt.Errorf(tpl, vars...)
}

// Errf new a raw go error. alias of errors.New()
func Errf(tpl string, vars ...any) error {
return fmt.Errorf(tpl, vars...)
}

// Rawf new a raw go error. alias of errors.New()
func Rawf(tpl string, vars ...any) error {
return fmt.Errorf(tpl, vars...)
Expand Down Expand Up @@ -54,7 +74,7 @@ func ToErrorX(err error) (ex *ErrorX, ok bool) {
return
}

// Has check err has contains target, or err is eq target.
// Has contains target error, or err is eq target.
// alias of errors.Is()
func Has(err, target error) bool {
return errors.Is(err, target)
Expand Down

0 comments on commit fb2a7c2

Please sign in to comment.