errs

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2020 License: GPL-3.0 Imports: 6 Imported by: 1

README

go-errs

Latest release Build status Go Report Card Documentation

Errs is a Go package designed to make working with (custom) errors an easy task. It supports adding stacktrace frames so tracing the cause of an error is a breeze.

go get github.com/roeldev/go-errs
import "github.com/roeldev/go-errs"

Creating an error

Wrapping existing errors

Error that is created with errs.New() and has an additional stack frame captured errs.Trace():

some error: something happened:
    main.doSomething
       .../go-errs/examples/2_trace/main.go:17
    main.someAction
       .../go-errs/examples/2_trace/main.go:12

A json unmarshalling error that's traced with errs.Trace():

some error: something bad happened while performing someAction:
    main.someAction
        .../go-errs/examples/3_trace_existing/main.go:22
  - invalid character 'i' looking for beginning of value:
        main.unmarshal
            .../go-errs/examples/3_trace_existing/main.go:16

Documentation

Additional detailed documentation is available at go.dev

Created with

License

GPL-3.0+ © 2019-2020 Roel Schut

Documentation

Index

Constants

View Source
const UnknownError string = "unknown error"

UnknownError is an error message that is returned when an error has no message and is of `UnknownKind`

Variables

View Source
var DefaultFramesCapacity uint = 5
View Source
var DefaultListCapacity uint = 8
View Source
var MustPanicFormat = "errs.Must: %+v"

MustPanicFormat is the template string used by the `Must()` function to format its panic message.

Functions

func Append added in v0.3.0

func Append(dest *error, err error) error

Append appends multiple non-nil errors to a single multi error `dest`.

Important: when using Append with defer, the pointer to the `dest` error must be a named return variable. For addition details see https://golang.org/ref/spec#Defer_statements.

func Combine added in v0.3.0

func Combine(errors ...error) error

Combine returns a multi error when there are more than one non-nil errors provided. If only one non-nil error is provided, it will act as if `TraceSkip` is called. It returns nil when all provided errors are nil.

func Filter added in v0.3.0

func Filter(errors []error) []error

Filter returns a slice of errors without nil values in between them. It returns the slice with the length of the amount of non-nil errors but keeps its original capacity.

func FormatError added in v0.2.0

func FormatError(err error, s fmt.State, v rune)

FormatError prints the error using `xerrors.FormatError()` and a formatter that implements the `xerrors.Formatter` interface. See the `golang.org/x/xerrors` package for additional information.

func Must added in v0.3.0

func Must(args ...interface{})

Must panics when any of the given args is a non-nil error. Its message is the error message of the first encountered error.

func New added in v0.2.0

func New(kind Kind, msg string) error

New creates a new error.

func Newf added in v0.2.0

func Newf(kind Kind, format string, a ...interface{}) error

Newf formats an error message according to a format specifier and provided arguments and creates a new error the same way `New()` does.

func Trace added in v0.2.0

func Trace(err error) error

Trace wraps an existing error with information about the stack frame its called from. Errors that implement the `ErrorWithStackTrace` interface add the frame to the existing stack trace. Other "simple" errors are wrapped in a `traceErr` type.

func TraceSkip added in v0.3.0

func TraceSkip(err error, skip uint) error

TraceSkip, just like Trace(), wraps an existing error with information about the stack frame its called from. The stack frame is selected based on the skip argument.

func UnwrapAll

func UnwrapAll(err error) []error

UnwrapAll returns the complete chain of errors, starting with the supplied error and ending with the error that started the chain.

func UnwrapCause

func UnwrapCause(err error) error

UnwrapCause walks through all wrapped errors and returns the last error in the chain which is the "cause" error.

func Wrap

func Wrap(cause error, kind Kind, msg string) error

Wrap creates a new error that wraps around the causing error, thus extending the error chain. In contrast to `New()`, it will only create a new error when the cause error is not `nil`.

func WrapPanic added in v0.2.0

func WrapPanic(prefix string)

WrapPanic wraps a panicking sequence with the given prefix. It then panics again.

func Wrapf

func Wrapf(cause error, kind Kind, format string, a ...interface{}) error

Wrapf formats an error message according to a format specifier and provided arguments and creates a new error the same way `Wrap()` does.

Types

type ErrLister added in v0.4.0

type ErrLister interface {
	// ErrList returns a List of collected non-nil errors that were encountered.
	ErrList() *List
}

type ErrorWithFrames added in v0.2.0

type ErrorWithFrames interface {
	error
	Frames() *Frames
}

ErrorWithFrames interfaces provide access to a stack of frames.

type ErrorWithKind

type ErrorWithKind interface {
	error
	Kind() Kind
}

ErrorWithKind interfaces provide access to a `Kind`.

type ErrorWithUnwrap

type ErrorWithUnwrap interface {
	error
	Unwrap() error
}

ErrorWithUnwrap interfaces provide access to an underlying error further down the error chain, if any.

type Frames added in v0.2.0

type Frames []xerrors.Frame

func CaptureFrames added in v0.2.0

func CaptureFrames(n uint, skip uint) Frames

CaptureFrames captures `n` frames starting from `skip`.

func GetFrames added in v0.2.0

func GetFrames(err error) *Frames

func (*Frames) Capture added in v0.2.0

func (fr *Frames) Capture(skip uint) (ok bool)

Capture captures a `xerrors.Frame` that describes a frame on the caller's stack. The argument skip is the number of frames to skip over. Capture(0) returns the frame for the caller of Capture. It returns a bool false when the captured frame contains a nil pointer.

func (Frames) Format added in v0.2.0

func (fr Frames) Format(p xerrors.Printer)

Format formats the captured frames using xerror's format functionality.

func (Frames) Len added in v0.3.0

func (fr Frames) Len() uint

Len returns the amount of captures frames as uint. Use `len()` when the value is needed as an int.

func (Frames) String added in v0.3.0

func (fr Frames) String() string

String formats the captured frames and returns its string representation.

type Inner added in v0.2.0

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

Inner is by itself not an error and is designed to be embedded in (custom) errors.

func MakeInner added in v0.2.0

func MakeInner(cause error, kind Kind) Inner

func (*Inner) Frames added in v0.2.0

func (e *Inner) Frames() *Frames

Frames returns a slice of captured `xerrors.Frame` types linked to this error.

func (*Inner) Kind added in v0.2.0

func (e *Inner) Kind() Kind

Kind returns the `Kind` of the error. It returns `UnknownKind` when no `Kind` is set.

func (*Inner) Unwrap added in v0.2.0

func (e *Inner) Unwrap() error

Unwrap returns the next error in the error chain. It returns `nil` if there is no next error.

type Kind

type Kind string

Kind describes the kind/type of error that has occurred. For example "auth error", "unmarshal error", etc. This way errors can be of the same `Kind` but still contain different error messages or additional fields. It is recommended to define each `Kind` as a constant.

const UnknownKind Kind = ""

UnknownKind is used for errors that are created without a distinct `Kind`.

func GetKind

func GetKind(err error) Kind

GetKind returns the `Kind` of the error if it implements the `ErrorWithKind` interface. If not, it returns `UnknownKind`.

func (Kind) String

func (k Kind) String() string

String returns the string representation of `Kind`.

type List added in v0.4.0

type List struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewList added in v0.4.0

func NewList(cap ...uint) *List

NewList creates a new List with a pre-allocated capacity of `cap`.

func (*List) All added in v0.4.0

func (l *List) All() []error

All returns the error slice within the list.

func (*List) Append added in v0.4.0

func (l *List) Append(err error) bool

Append an error to the list. It guarantees only non-nil errors are added. It returns `false` when a nil error is encountered. And `true` when the error is appended to the list.

func (*List) Combine added in v0.4.0

func (l *List) Combine() error

Combine the collected errors. It uses the same rules and logic as the `Combine` function.

func (*List) Len added in v0.4.0

func (l *List) Len() int

Len returns the number of errors within the list.

func (*List) Prepend added in v0.4.0

func (l *List) Prepend(err error) bool

Prepend an error to the list. It guarantees only non-nil errors are added. It returns `false` when a nil error is encountered. And `true` when the error is prepended to the list.

type MultiError added in v0.4.0

type MultiError interface {
	error
	Errors() []error
}

type WaitGroup added in v0.4.0

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

A WaitGroup is a collection of goroutines working on subtasks that are part of the same overall task. Unlike `errgroup.Group`, it collects possible errors returned from the subtasks and does not cancel the group when an error is encountered.

func (*WaitGroup) ErrList added in v0.4.0

func (g *WaitGroup) ErrList() *List

ErrList returns a List of collected errors from the called goroutines.

func (*WaitGroup) Go added in v0.4.0

func (g *WaitGroup) Go(fn func() error)

Go calls the given function in a new goroutine. Errors from all calls are collected, combined and returned by Wait.

func (*WaitGroup) Wait added in v0.4.0

func (g *WaitGroup) Wait() error

Wait blocks until all function calls from the Go method have returned, then returns all collected errors as a combined (multi) error.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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