Documentation ¶
Index ¶
- Variables
- func Handle(errp *error, args ...error)
- func TestWrapFunc(t *testing.T, fn WrapFunc)
- func Throw(err error, args ...error) error
- type CheckFunc
- type ErrInfo
- type Error
- type Frame
- type Stacktrace
- type WrapFunc
- func Close(c io.Closer) 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
- func WrapStacktraceWithoutPackageName(names ...string) 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 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 n := runtime.Callers(2, pcs) stacktrace.Frames = make([]Frame, 0, n) frames := runtime.CallersFrames(pcs[:n]) for { frame, more := frames.Next() if strings.HasPrefix(frame.Function, "github.com/reusee/e5.") && !strings.HasPrefix(frame.Function, "github.com/reusee/e5.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 stacktrace.Frames = append(stacktrace.Frames, Frame{ File: file, Dir: dir, Line: frame.Line, Pkg: pkg, Function: fn, PkgPath: pkgPath, }) if !more { break } } err := Join(stacktrace, prev) return err })
WrapStacktrace wraps current stacktrace
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 joined. 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 CheckFunc ¶
CheckFunc is the type of Check, see Check's doc for details
type ErrInfo ¶
type ErrInfo struct {
// contains filtered or unexported fields
}
ErrInfo represents a lazy-evaluaed formatted string
type Error ¶
type Error []error
Error represents multiple errors
func (Error) As ¶
As reports whether any error in the slice matches target. And if so, assign the first matching error to target
type Stacktrace ¶
type Stacktrace struct {
Frames []Frame
}
Stacktrace represents call stack frames
func (*Stacktrace) Is ¶
func (s *Stacktrace) Is(err error) bool
type WrapFunc ¶
WrapFunc wraps an error to form a new one.
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, a new one will be created
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