errors

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2022 License: MIT Imports: 7 Imported by: 3

README

simple but powerful errors library that allows providing context information with errors

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target interface{}) bool

As wraps "errors".As().

func Is

func Is(err, target error) bool

Is wraps "errors".Is().

func Unwrap

func Unwrap(err error) error

Unwrap wraps "errors".Unwrap().

func UnwrapAll added in v1.0.2

func UnwrapAll(err error) string

UnwrapAll fully unwraps an error stack to produce a string output.

Types

type ErrorContext

type ErrorContext interface {
	// implement base error interface
	error

	// Is identifies whether the receiver contains / is the target
	Is(error) bool

	// Unwrap reveals the underlying wrapped error (if any!)
	Unwrap() error

	// Value attempts to fetch contextual data for given key from this ErrorContext
	Value(string) (interface{}, bool)

	// Append allows adding contextual data to this ErrorContext
	Append(...kv.Field) ErrorContext

	// Data returns the contextual data structure associated with this ErrorContext
	Data() ErrorData
}

ErrorContext defines a wrappable error with the ability to hold extra contextual information.

func New

func New(msg string) ErrorContext

New returns a new ErrorContext created from string.

func Newf

func Newf(s string, a ...interface{}) ErrorContext

Newf returns a new ErrorContext created from format string.

func Wrap

func Wrap(err error) ErrorContext

Wrap ensures supplied error is an ErrorContext, wrapping if necessary.

func WrapMsg

func WrapMsg(err error, msg string) ErrorContext

WrapMsg wraps supplied error as inner, returning an ErrorContext with a new outer error made from the supplied message string.

func WrapMsgf

func WrapMsgf(err error, msg string, a ...interface{}) ErrorContext

WrapMsgf wraps supplied error as inner, returning an ErrorContext with a new outer error made from the supplied message format string.

type ErrorData

type ErrorData interface {
	// Value will attempt to fetch value for given key in ErrorData
	Value(string) (interface{}, bool)

	// Append adds the supplied key-values to ErrorData, similar keys DO overwrite
	Append(...kv.Field)
}

ErrorData defines a way to set and access contextual error data. The default implementation of this is thread-safe.

func Data

func Data(err error) ErrorData

ErrorData attempts fetch ErrorData from supplied error, returns nil otherwise.

func NewData

func NewData() ErrorData

NewData returns a new ErrorData implementation.

type OnceError

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

OnceError is an error structure that supports safe multi threaded usage and setting only once (until reset).

func NewOnce

func NewOnce() OnceError

NewOnce returns a new OnceError instance.

func (*OnceError) IsSet

func (e *OnceError) IsSet() bool

IsSet returns whether OnceError has been set.

func (*OnceError) Load

func (e *OnceError) Load() error

Load will load the currently stored error.

func (*OnceError) Reset

func (e *OnceError) Reset()

Reset will reset the OnceError value.

func (*OnceError) Store

func (e *OnceError) Store(err error)

Store will safely set the OnceError to value.

Jump to

Keyboard shortcuts

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