errorz

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2022 License: MIT Imports: 5 Imported by: 21

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assertf

func Assertf(cond bool, format string, options ...Option)

Assertf is like MustErrorf if cond is false, does nothing otherwise.

func Errorf

func Errorf(format string, options ...Option) error

Errorf formats a new error and wraps it. Note: arguments implementing Option are applied on wrapping, the others are passed to fmt.Errorf().

func FormatStackTrace

func FormatStackTrace(callers []uintptr) []string

FormatStackTrace formats the given raw stack trace.

func GetCallers

func GetCallers(err error) []uintptr

GetCallers returns the raw stack trace from the error, or the current raw stack trace if not found.

func IgnoreClose

func IgnoreClose(c io.Closer)

IgnoreClose calls Close on the given io.Closer, ignoring the returned error. Handy for the defer Close pattern.

func MaybeMustWrap

func MaybeMustWrap(err error, options ...Option)

MaybeMustWrap is like MustWrap, but does nothing if called with a nil error.

func MaybeWrap

func MaybeWrap(err error, options ...Option) error

MaybeWrap is like Wrap, but returns nil if called with a nil error.

func MaybeWrapRecover

func MaybeWrapRecover(r interface{}, options ...Option) error

MaybeWrapRecover is like WrapRecover but returns nil if called with a nil recover.

func MustClose

func MustClose(c io.Closer)

MustClose calls Close on the given io.Closer, panicking in case of error. Handy for the defer Close pattern.

func MustErrorf

func MustErrorf(format string, options ...Option)

MustErrorf is like Errorf but panics instead of returning the error.

func MustWrap

func MustWrap(err error, options ...Option)

MustWrap is like Wrap, but panics if the given error is non-nil.

func Safe

func Safe(f func() error) func() error

Safe calls the function catching any panic and returning it as error.

func Unwrap

func Unwrap(err error) error

Unwrap undoes Wrap, returning the original error.

func Wrap

func Wrap(err error, options ...Option) error

Wrap wraps the given error, applying the given options.

func WrapRecover

func WrapRecover(r interface{}, options ...Option) error

WrapRecover takes a recovered interface{} and converts it to a wrapped error.

Types

type Args

type Args []interface{}

Args describes a list of args used for formatting an error message.

func A

func A(a ...interface{}) Args

A is a shorthand builder for args.

func (Args) Apply

func (a Args) Apply(_ error)

Apply implements the Option interface.

type ID

type ID string

ID describes an error id.

func GetID

func GetID(err error) ID

GetID gets the id from the error, or an empty id if not set.

func (ID) Apply

func (id ID) Apply(err error)

Apply implements the Option interface.

func (ID) String

func (id ID) String() string

String implements the fmt.Stringer interface.

type Metadata

type Metadata map[string]interface{}

Metadata describes metadata which can be attached to errors.

func GetMetadata

func GetMetadata(err error) Metadata

GetMetadata gets the metadata from the error, nil if not found.

func (Metadata) Apply

func (m Metadata) Apply(err error)

Apply implements the Option interface.

func (Metadata) Get

func (m Metadata) Get(k string) interface{}

Get gets a value from Metadata, nil if not found or Metadata is nil.

func (Metadata) GetString

func (m Metadata) GetString(k string) string

GetString gets a value from Metadata as string, empty if not found or of different type.

type Option

type Option interface {
	Apply(err error)
}

Option describes an option which can be applied to an error.

type OptionFunc

type OptionFunc func(err error)

OptionFunc describes an option which can be applied to an error.

func M

func M(k string, v interface{}) OptionFunc

M is a shorthand for providing metadata to errors.

func Prefix

func Prefix(format string, a ...interface{}) OptionFunc

Prefix adds a prefix to the error message.

func Skip

func Skip() OptionFunc

Skip skips the caller from the stack trace.

func SkipAll

func SkipAll() OptionFunc

SkipAll skips the caller and any lower frames from the stack trace.

func SkipPackage added in v1.1.0

func SkipPackage() OptionFunc

SkipPackage skips all frames from the caller package from the stack trace.

func (OptionFunc) Apply

func (f OptionFunc) Apply(err error)

Apply implements the Option interface.

type Status

type Status int

Status describes an HTTP status code.

func GetStatus

func GetStatus(err error) Status

GetStatus gets the status code from the error, or 0 if not set.

func (Status) Apply

func (s Status) Apply(err error)

Apply implements the Option interface.

func (Status) Int

func (s Status) Int() int

Int returns the status code as "int".

type Summary

type Summary struct {
	ID         ID                     `json:"id,omitempty" yaml:"id,omitempty"`
	Status     Status                 `json:"status,omitempty" yaml:"id,omitempty"`
	Metadata   map[string]interface{} `json:"metadata,omitempty" yaml:"id,omitempty"`
	Message    string                 `json:"message,omitempty" yaml:"id,omitempty"`
	StackTrace []string               `json:"stackTrace,omitempty" yaml:"id,omitempty"`
}

Summary provides a serializable summary of an error and its metadata.

func ToSummary

func ToSummary(err error) *Summary

ToSummary converts an error to Summary.

Jump to

Keyboard shortcuts

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