Documentation
¶
Overview ¶
Example (StackTrace) ¶
package main
import (
"errors"
"fmt"
"github.com/fengjx/go-halo/errs"
)
func fn() error {
e1 := errors.New("error")
e2 := errs.Wrap(e1, "inner")
e3 := errs.Wrap(e2, "middle")
return errs.Wrap(e3, "outer")
}
func main() {
type stackTracer interface {
StackTrace() errs.StackTrace
}
err, ok := errs.Cause(fn()).(stackTracer)
if !ok {
panic("oops, err does not implement stackTracer")
}
st := err.StackTrace()
fmt.Printf("%+v", st[0:2]) // top two frames
// Example output:
// github.com/fengjx/go-halo/errs_test.fn
// /home/dfc/src/github.com/fengjx/go-halo/errs/example_test.go:47
// github.com/fengjx/go-halo/errs_test.Example_stackTrace
// /home/dfc/src/github.com/fengjx/go-halo/errs/example_test.go:127
}
Output:
Index ¶
- func Cause(err error) error
- func Recover()
- func RecoverFunc(fn RecoverHandle)
- func RegisterRecoverHandle(fn RecoverHandle)
- func WithStack(err error) error
- func Wrap(err error, msg string) error
- func Wrapf(err error, format string, args ...any) error
- type Frame
- type RecoverHandle
- type Stack
- type StackTrace
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cause ¶
Cause 返回根因 error 查找第一个没有实现 causer 的 error,认为是根因
Example ¶
package main
import (
"errors"
"fmt"
"github.com/fengjx/go-halo/errs"
)
func fn() error {
e1 := errors.New("error")
e2 := errs.Wrap(e1, "inner")
e3 := errs.Wrap(e2, "middle")
return errs.Wrap(e3, "outer")
}
func main() {
err := fn()
fmt.Println(err)
fmt.Println(errs.Cause(err))
}
Output: outer: middle: inner: error error
Example (Printf) ¶
package main
import (
"errors"
"fmt"
"github.com/fengjx/go-halo/errs"
)
func main() {
err := errs.Wrap(func() error {
return func() error {
return errors.New("hello world")
}()
}(), "failed")
fmt.Printf("%v", err)
}
Output: failed: hello world
func Recover ¶
func Recover()
Recover recover 处理,打印堆栈 直接defer errs.Recover() 而不能defer func(){errs.Recover()}
func RecoverFunc ¶
func RecoverFunc(fn RecoverHandle)
func RegisterRecoverHandle ¶
func RegisterRecoverHandle(fn RecoverHandle)
RegisterRecoverHandle 自定义的recover处理函数
func WithStack ¶
WithStack 给错误添加堆栈信息
Example ¶
package main
import (
"errors"
"fmt"
"github.com/fengjx/go-halo/errs"
)
func main() {
cause := errors.New("whoops")
err := errs.WithStack(cause)
fmt.Println(err)
}
Output: whoops
Example (Printf) ¶
package main
import (
"errors"
"fmt"
"github.com/fengjx/go-halo/errs"
)
func main() {
cause := errors.New("whoops")
err := errs.WithStack(cause)
fmt.Printf("%+v", err)
// Example Output:
// whoops
// github.com/fengjx/go-halo/errs_test.ExampleWithStack_printf
// /home/fabstu/go/src/github.com/fengjx/go-halo/errs/example_test.go:55
// testing.runExample
// /usr/lib/go/src/testing/example.go:114
// testing.RunExamples
// /usr/lib/go/src/testing/example.go:38
// testing.(*M).Run
// /usr/lib/go/src/testing/testing.go:744
// main.main
// github.com/fengjx/go-halo/errs/_test/_testmain.go:106
// runtime.main
// /usr/lib/go/src/runtime/proc.go:183
// runtime.goexit
// /usr/lib/go/src/runtime/asm_amd64.s:2086
// github.com/fengjx/go-halo/errs_test.ExampleWithStack_printf
// /home/fabstu/go/src/github.com/fengjx/go-halo/errs/example_test.go:56
// testing.runExample
// /usr/lib/go/src/testing/example.go:114
// testing.RunExamples
// /usr/lib/go/src/testing/example.go:38
// testing.(*M).Run
// /usr/lib/go/src/testing/testing.go:744
// main.main
// github.com/fengjx/go-halo/errs/_test/_testmain.go:106
// runtime.main
// /usr/lib/go/src/runtime/proc.go:183
// runtime.goexit
// /usr/lib/go/src/runtime/asm_amd64.s:2086
}
Output:
func Wrap ¶
Wrap 包装错误信息,并且保存堆栈信息
Example ¶
package main
import (
"errors"
"fmt"
"github.com/fengjx/go-halo/errs"
)
func main() {
cause := errors.New("whoops")
err := errs.Wrap(cause, "oh noes")
fmt.Println(err)
}
Output: oh noes: whoops
Example (Extended) ¶
package main
import (
"errors"
"fmt"
"github.com/fengjx/go-halo/errs"
)
func fn() error {
e1 := errors.New("error")
e2 := errs.Wrap(e1, "inner")
e3 := errs.Wrap(e2, "middle")
return errs.Wrap(e3, "outer")
}
func main() {
err := fn()
fmt.Printf("%+v\n", err)
// Example output:
// error
// github.com/fengjx/go-halo/errs_test.fn
// /home/dfc/src/github.com/fengjx/go-halo/errs/example_test.go:47
// github.com/fengjx/go-halo/errs_test.ExampleCause_printf
// /home/dfc/src/github.com/fengjx/go-halo/errs/example_test.go:63
// testing.runExample
// /home/dfc/go/src/testing/example.go:114
// testing.RunExamples
// /home/dfc/go/src/testing/example.go:38
// testing.(*M).Run
// /home/dfc/go/src/testing/testing.go:744
// main.main
// /github.com/fengjx/go-halo/errs/_test/_testmain.go:104
// runtime.main
// /home/dfc/go/src/runtime/proc.go:183
// runtime.goexit
// /home/dfc/go/src/runtime/asm_amd64.s:2059
// github.com/fengjx/go-halo/errs_test.fn
// /home/dfc/src/github.com/fengjx/go-halo/errs/example_test.go:48: inner
// github.com/fengjx/go-halo/errs_test.fn
// /home/dfc/src/github.com/fengjx/go-halo/errs/example_test.go:49: middle
// github.com/fengjx/go-halo/errs_test.fn
// /home/dfc/src/github.com/fengjx/go-halo/errs/example_test.go:50: outer
}
Output:
Types ¶
type Frame ¶
func (Frame) MarshalText ¶
MarshalText formats a stacktrace Frame as a text string. The output is the same as that of fmt.Sprintf("%+v", f), but without newlines or tabs.
type RecoverHandle ¶
Click to show internal directories.
Click to hide internal directories.