e5

package module
v0.0.0-...-9deb1a7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2023 License: Apache-2.0 Imports: 10 Imported by: 37

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

View Source
var Wrap = WrapFunc(func(err error) error {
	return err
})
View Source
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

func Handle(errp *error, args ...error)

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

func TestWrapFunc(t *testing.T, fn WrapFunc)

TestWrapFunc tests a WrapFunc instance

func Throw

func Throw(err error, args ...error) error

Throw checks the error and if not nil, raise a panic which will be recovered by Handle

Types

type CheckFunc

type CheckFunc func(err error, args ...error) error

CheckFunc is the type of Check, see Check's doc for details

func (CheckFunc) With

func (c CheckFunc) With(moreArgs ...error) CheckFunc

With returns a new CheckFunc that do additional wrapping with moreWraps

func (CheckFunc) WithInfo

func (c CheckFunc) WithInfo(format string, args ...any) CheckFunc

type ErrInfo

type ErrInfo struct {
	// contains filtered or unexported fields
}

ErrInfo represents a lazy-evaluaed formatted string

func (*ErrInfo) Error

func (i *ErrInfo) Error() string

Error implements error interface

type Error

type Error []error

Error represents multiple errors

func Join

func Join(err error, prev error) Error

Join joins two errors

func (Error) As

func (c Error) As(target interface{}) bool

As reports whether any error in the slice matches target. And if so, assign the first matching error to target

func (Error) Error

func (c Error) Error() string

Error implements error interface

func (Error) Is

func (c Error) Is(target error) bool

Is reports whether any error in the slice matches target

func (Error) Unwrap

func (c Error) Unwrap() []error

Unwrap returns all errors

type Frame

type Frame struct {
	File     string
	Dir      string
	Pkg      string
	Function string
	PkgPath  string
	Line     int
}

Frame represents a call frame

type Stacktrace

type Stacktrace struct {
	Frames []Frame
}

Stacktrace represents call stack frames

func (*Stacktrace) Error

func (s *Stacktrace) Error() string

Error implements error interface

func (*Stacktrace) Is

func (s *Stacktrace) Is(err error) bool

type WrapFunc

type WrapFunc func(err error) error

WrapFunc wraps an error to form a new one.

Instances must follow these rules: if argument is nil, return value must be nil

func Close

func Close(c io.Closer) WrapFunc

Close returns a WrapFunc that closes the Closer

func Do

func Do(fn func()) WrapFunc

Do returns a WrapFunc that calls fn

func DropFrame

func DropFrame(fn func(Frame) bool) WrapFunc

DropFrame returns a WrapFunc that drop Frames matching fn. If there is no existed stacktrace, a new one will be created

func Ignore

func Ignore(err error) WrapFunc

Ignore returns a WrapFunc that returns nil if errors.Is(prev, err) is true

func IgnoreAs

func IgnoreAs(target any) WrapFunc

IgnoreAs returns a WrapFunc that returns nil if errors.As(prev, target) is true

func IgnoreContains

func IgnoreContains(str string) WrapFunc

IgnoreContains returns a WrapFunc that returns nil if prev.Error() contains str

func Info

func Info(format string, args ...any) WrapFunc

Info returns a WrapFunc that wraps an *ErrInfo error value

func TestingFatal

func TestingFatal(t *testing.T) WrapFunc

TestingFatal returns a WrapFunc that calls t.Fatal if error occur

func With

func With(err error) WrapFunc

With returns a WrapFunc that wraps an error value

func WrapStacktraceWithoutPackageName

func WrapStacktraceWithoutPackageName(names ...string) WrapFunc

func (WrapFunc) Assign

func (w WrapFunc) Assign(p *error)

func (WrapFunc) Error

func (w WrapFunc) Error() string

func (WrapFunc) With

func (w WrapFunc) With(args ...error) WrapFunc

func (WrapFunc) WithInfo

func (w WrapFunc) WithInfo(format string, args ...any) WrapFunc

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL