errors

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2021 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()

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) 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)

	// String returns a string representation of the ErrorData
	String() string
}

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 KV

type KV struct {
	Key   string
	Value interface{}
}

KV is a structure for setting key-value pairs in ErrorData

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

func (*OnceError) Load

func (e *OnceError) Load() error

func (*OnceError) Reset

func (e *OnceError) Reset()

func (*OnceError) Store

func (e *OnceError) Store(err error)

Jump to

Keyboard shortcuts

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