zerrors

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2021 License: MIT Imports: 7 Imported by: 1

README

zerrors

Go Reference CodeFactor Go Unit Tests CodeQL codecov

NOTICE: This is work in progress. Do not use!

Installation

go get -u github.com/dtomasi/zerrors

Usage example

TBD

Documentation

Overview

Package zerrors is a wrapper around golang.org/x/xerrors with some additional features.

Index

Constants

View Source
const GenericError = ErrorType("GenericError")

GenericError is the default error type.

Variables

View Source
var DefaultSkipCallers = 0 //nolint:gochecknoglobals

DefaultSkipCallers exposes the default value that should be used for caller frame skipping This can be adjusted within an init function.

Functions

func As

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

As see: https://pkg.go.dev/golang.org/x/xerrors#As

func Errorf

func Errorf(format string, a ...interface{}) error

Errorf see: https://pkg.go.dev/golang.org/x/xerrors#Errorf

func FormatError

func FormatError(f Formatter, s fmt.State, verb rune)

FormatError see: https://pkg.go.dev/golang.org/x/xerrors#FormatError

func Is

func Is(err error, target error) bool

Is see: https://pkg.go.dev/golang.org/x/xerrors#Is

func IsType

func IsType(err TypeAwareError, errType fmt.Stringer) bool

IsType Check if a given error is matching given type Example:

zerrors.IsType(err, MyErrorTypeWithStringerInterface)

func Mask

func Mask(err error) error

Mask simply masks an error with no message.

func New

func New(msg string) error

New creates a new error with message.

func NewWithOpts

func NewWithOpts(msg string, opts ...ErrorOpt) error

NewWithOpts creates a new error with optional passed options see: ./options.go for available options

func Newf

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

Newf creates a new error with formatted message.

func WalkErrChain

func WalkErrChain(err error, f func(err error) interface{}) interface{}

WalkErrChain provides a simple way to walk e error chain using Unwrap function.

func Wrap

func Wrap(err error, msg string) error

Wrap wraps a given error with a new zError instance.

func WrapPtr

func WrapPtr(errp *error, msg string)

WrapPtr wraps a given error pointer with a new zError instance This is useful for defer used with named return types.

func WrapPtrWithOpts

func WrapPtrWithOpts(errp *error, msg string, opts ...ErrorOpt)

WrapPtrWithOpts wraps an error pointer.

func WrapPtrf

func WrapPtrf(errp *error, format string, args ...interface{})

WrapPtrf wraps a given error pointer with a new zError instance and allows format message string This is useful for defer used with named return types.

func WrapWithOpts

func WrapWithOpts(err error, msg string, opts ...ErrorOpt) error

WrapWithOpts wraps an error.

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

Wrapf wraps a given error with a new zError instance and allows format message string.

Types

type ContextAwareError

type ContextAwareError interface {
	// implement error interface here
	error
	// Context does a recursive lookup for context.Context and returns the first contexts in err´s chain
	Context() context.Context
	// ContextValue does a recursive lookup for a context value for given key. It returns nil if no value can be found.
	ContextValue(key interface{}) interface{}
}

ContextAwareError defines an error that contains an context.

type ErrorOpt

type ErrorOpt func(e *zError)

ErrorOpt defines the option function.

func WithContext

func WithContext(ctx context.Context) ErrorOpt

WithContext allows passing a context to the error.

func WithContextValue

func WithContextValue(key interface{}, value interface{}) ErrorOpt

WithContextValue allows passing a key/value pair to the error, which is stored inside the context.Context NOTE: If no context is defined while this option is applied, it will create an empty context using context.TODO().

func WithSkipCallers

func WithSkipCallers(skip int) ErrorOpt

WithSkipCallers allows defining how many caller frames should be skipped. This only applies to output format `%+v` that prints the stacktrace.

func WithType

func WithType(errType fmt.Stringer) ErrorOpt

WithType allows to pass a type (fmt.Stringer compatible) to the error.

func WithWrappedError

func WithWrappedError(err error) ErrorOpt

WithWrappedError allows to pass an error that should be wrapped.

type ErrorType

type ErrorType string

ErrorType defines an easy to use fmt.Stringer compatible error type. Example:

zerrors.ErrorType("MyNamedErrorType")

func (ErrorType) String

func (t ErrorType) String() string

String returns the ErrorType string. This function is required to implement fmt.Stringer interface.

type TypeAwareError

type TypeAwareError interface {
	// implement error interface here
	error
	// Type returns the ErrorType
	Type() fmt.Stringer
	// IsType checks if error is of passed ErrorType
	IsType(errType fmt.Stringer) bool
}

TypeAwareError defines an error that is aware of its type.

Jump to

Keyboard shortcuts

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