Documentation
¶
Index ¶
- Variables
- func Handle(errp *error, args ...error)
- func Must(err error, args ...error) error
- func TestWrapFunc(t *testing.T, fn WrapFunc)
- func Throw(err error, args ...error) error
- func Try[R any](r R, err error) func(fn CheckFunc) R
- func Try2[R, R2 any](r R, r2 R2, err error) func(fn CheckFunc) (R, R2)
- func Try3[R, R2, R3 any](r R, r2 R2, r3 R3, err error) func(fn CheckFunc) (R, R2, R3)
- func Try4[R, R2, R3, R4 any](r R, r2 R2, r3 R3, r4 R4, err error) func(fn CheckFunc) (R, R2, R3, R4)
- type CheckFunc
- type ErrDebug
- type ErrInfo
- type Error
- type ErrorLeveler
- type Frame
- type Level
- type Stacktrace
- type WrapFunc
- func Close(c io.Closer) WrapFunc
- func Debug(format string, args ...any) WrapFunc
- func Do(fn func()) WrapFunc
- func DropFrame(fn func(Frame) bool) WrapFunc
- func Ignore(err error) WrapFunc
- func IgnoreAs(target any) WrapFunc
- func IgnoreContains(str string) WrapFunc
- func Info(format string, args ...any) WrapFunc
- func TestingFatal(t *testing.T) WrapFunc
- func With(err error) WrapFunc
Constants ¶
This section is empty.
Variables ¶
var Check = CheckFunc(func(err error, args ...error) error { if err == nil { return nil } err = Wrap.With(args...)(err) return Throw(err) })
Check is for error checking. if err is not nil, it will be wrapped by DefaultWrap then raised by Throw
var CheckWithStacktrace = Check.With(WrapStacktrace)
var ErrorLevel = InfoLevel
var Fatal = Must
var NewDebug = Debug
var NewInfo = Info
var Wrap = WrapFunc(func(err error) error {
return err
})
var WrapStacktrace = WrapFunc(func(prev error) error { if prev == nil { return nil } if stacktraceIncluded(prev) { return prev } stacktrace := new(Stacktrace) v, put := pcsPool.Get() defer put() pcs := *(v.(*[]uintptr)) skip := 1 for { n := runtime.Callers(skip, pcs) if n == 0 { break } for i := 0; i < n; i++ { var slice []Frame frames := runtime.CallersFrames(pcs[i : i+1]) for { skip++ frame, more := frames.Next() if strings.HasPrefix(frame.Function, "github.com/reusee/e4.") && !strings.HasPrefix(frame.Function, "github.com/reusee/e4.Test") { if !more { break } continue } dir, file := filepath.Split(frame.File) mod, fn := path.Split(frame.Function) if i := strings.Index(dir, mod); i > 0 { dir = dir[i:] } pkg := fn[:strings.IndexByte(fn, '.')] pkgPath := mod + pkg f := Frame{ File: file, Dir: dir, Line: frame.Line, Pkg: pkg, Function: fn, PkgPath: pkgPath, } slice = append(slice, f) if !more { break } } stacktrace.Frames = append(stacktrace.Frames, slice...) } if n < len(pcs) { break } } err := MakeErr(stacktrace, prev) err.flag |= flagStacktraceIncluded return err })
WrapStacktrace wraps current stacktrace
var WrapWithStacktrace = Wrap.With(WrapStacktrace)
Functions ¶
func Handle ¶
Handle is for error handling
Error raised by Throw will be catched if any. If errp point to non-nil error, the error will be chained. If the result error is not nil, wrap functions will be applied. The result error will be assigned to errp if errp is not nil, otherwise Throw will be raised.
func TestWrapFunc ¶
TestWrapFunc tests a WrapFunc instance
Types ¶
type ErrInfo ¶
type ErrInfo struct {
// contains filtered or unexported fields
}
ErrInfo represents a lazy-evaluaed formatted string
type Error ¶
Error represents a chain of errors
func MakeErr ¶
MakeErr creates an Error with internal manipulations. It's safe to construct Error without calling this function but not encouraged
func (Error) As ¶
As reports whether any error in the chain matches target. And if so, assign the first matching error to target
type ErrorLeveler ¶
type ErrorLeveler interface {
ErrorLevel() Level
}
type WrapFunc ¶
WrapFunc wraps an error to form a chain.
Instances must follow these rules: if argument is nil, return value must be nil
func DropFrame ¶
DropFrame returns a WrapFunc that drop Frames matching fn. If there is no existed stacktrace in chain, a new one will be created
func IgnoreAs ¶
func IgnoreAs(target any) WrapFunc
IgnoreAs returns a WrapFunc that returns nil if errors.As(prev, target) is true
func IgnoreContains ¶
IgnoreContains returns a WrapFunc that returns nil if prev.Error() contains str
func TestingFatal ¶
TestingFatal returns a WrapFunc that calls t.Fatal if error occur