Skip to content

Latest commit

 

History

History

errorx

ErrorX

errorx provide an enhanced error implements for go, allow with stacktraces and wrap another error.

Install

go get github.com/gookit/goutil/errorx

Go docs

Usage

Create error with call stack info

  • use the errorx.New instead errors.New
func doSomething() error {
    if false {
	    // return errors.New("a error happen")
	    return errorx.New("a error happen")
	}
}
  • use the errorx.Newf or errorx.Errorf instead fmt.Errorf
func doSomething() error {
    if false {
	    // return fmt.Errorf("a error %s", "happen")
	    return errorx.Newf("a error %s", "happen")
	}
}

Wrap the previous error

used like this before:

    if err := SomeFunc(); err != nil {
	    return err
	}

can be replaced with:

    if err := SomeFunc(); err != nil {
	    return errors.Stacked(err)
	}

Output details

error output details for use errorx

Use errorx.New

errorx functions for new error:

func New(msg string) error
func Newf(tpl string, vars ...interface{}) error
func Errorf(tpl string, vars ...interface{}) error

Examples:

    err := errorx.New("the error message")

    fmt.Println(err)
    // fmt.Printf("%v\n", err)
    // fmt.Printf("%#v\n", err)

from the test: errorx_test.TestNew()

Output:

the error message
STACK:
github.com/gookit/goutil/errorx_test.returnXErr()
  /Users/inhere/Workspace/godev/gookit/goutil/errorx/errorx_test.go:21
github.com/gookit/goutil/errorx_test.returnXErrL2()
  /Users/inhere/Workspace/godev/gookit/goutil/errorx/errorx_test.go:25
github.com/gookit/goutil/errorx_test.TestNew()
  /Users/inhere/Workspace/godev/gookit/goutil/errorx/errorx_test.go:29
testing.tRunner()
  /usr/local/Cellar/go/1.18/libexec/src/testing/testing.go:1439
runtime.goexit()
  /usr/local/Cellar/go/1.18/libexec/src/runtime/asm_amd64.s:1571

Use errorx.With

errorx functions for with another error:

func With(err error, msg string) error
func Withf(err error, tpl string, vars ...interface{}) error

With a go raw error:

	err1 := returnErr("first error message")

	err2 := errorx.With(err1, "second error message")
	fmt.Println(err2)

from the test: errorx_test.TestWith_goerr()

Output:

second error message
STACK:
github.com/gookit/goutil/errorx_test.TestWith_goerr()
  /Users/inhere/Workspace/godev/gookit/goutil/errorx/errorx_test.go:51
testing.tRunner()
  /usr/local/Cellar/go/1.18/libexec/src/testing/testing.go:1439
runtime.goexit()
  /usr/local/Cellar/go/1.18/libexec/src/runtime/asm_amd64.s:1571

Previous: first error message

With a errorx error:

	err1 := returnXErr("first error message")
	err2 := errorx.With(err1, "second error message")
	fmt.Println(err2)

from the test: errorx_test.TestWith_errorx()

Output:

second error message
STACK:
github.com/gookit/goutil/errorx_test.TestWith_errorx()
  /Users/inhere/Workspace/godev/gookit/goutil/errorx/errorx_test.go:64
testing.tRunner()
  /usr/local/Cellar/go/1.18/libexec/src/testing/testing.go:1439
runtime.goexit()
  /usr/local/Cellar/go/1.18/libexec/src/runtime/asm_amd64.s:1571

Previous: first error message
STACK:
github.com/gookit/goutil/errorx_test.returnXErr()
  /Users/inhere/Workspace/godev/gookit/goutil/errorx/errorx_test.go:21
github.com/gookit/goutil/errorx_test.TestWith_errorx()
  /Users/inhere/Workspace/godev/gookit/goutil/errorx/errorx_test.go:61
testing.tRunner()
  /usr/local/Cellar/go/1.18/libexec/src/testing/testing.go:1439
runtime.goexit()
  /usr/local/Cellar/go/1.18/libexec/src/runtime/asm_amd64.s:1571

Use errorx.Wrap

err := errors.New("first error message")
err = errorx.Wrap(err, "second error message")
err = errorx.Wrap(err, "third error message")
// fmt.Println(err)
// fmt.Println(err.Error())

Direct print the err:

third error message
Previous: second error message
Previous: first error message

Print the err.Error():

third error message; second error message; first error message

Code Check & Testing

gofmt -w -l ./
golint ./...

Testing:

go test -v ./errorx/...

Test limit by regexp:

go test -v -run ^TestSetByKeys ./errorx/...

Refers