errorx
provide an enhanced error implements for go, allow with stacktraces and wrap another error.
go get github.com/gookit/goutil/errorx
- use the
errorx.New
insteaderrors.New
func doSomething() error {
if false {
// return errors.New("a error happen")
return errorx.New("a error happen")
}
}
- use the
errorx.Newf
orerrorx.Errorf
insteadfmt.Errorf
func doSomething() error {
if false {
// return fmt.Errorf("a error %s", "happen")
return errorx.Newf("a error %s", "happen")
}
}
used like this before:
if err := SomeFunc(); err != nil {
return err
}
can be replaced with:
if err := SomeFunc(); err != nil {
return errors.Stacked(err)
}
error output details for use errorx
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
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
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
gofmt -w -l ./
golint ./...
Testing:
go test -v ./errorx/...
Test limit by regexp:
go test -v -run ^TestSetByKeys ./errorx/...