failure

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2020 License: BSD-3-Clause Imports: 4 Imported by: 16

Documentation

Overview

Package failure allows to create more detailed error values than with errors.New() or fmt.Errorf().

The errors package allows to easily created formatted errors with New() like with the fmt.Errorf() function, but also containing the location and a code. The creation of error nessages is like with fmt.Errorf().

If an error alreay exists use Annotate(). This way the original error will be stored and can be retrieved with Annotated(). Also its error message will be appended to the created error separated by a colon.

All errors additionally contain their package, filename and line number. These information can be retrieved using Location(). In case of a chain of annotated errors those can be retrieved as a slice of errors with Stack().

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(err error) []error

All returns a slice of errors in case of collected errors.

func Annotate

func Annotate(err error, msg string, args ...interface{}) error

Annotate creates an error wrapping another one together with a a code. If the passed one is nil, Annotate() also returns nil.

func Annotated

func Annotated(err error) error

Annotated returns the possibly annotated error. In case of a different error an invalid type error is returned.

func Collect

func Collect(errs ...error) error

Collect collects multiple errors into one. If there's none, nil will be returned.

func Contains

func Contains(err error, substr string) bool

Contains returns true if the given error contains a given string.

func DoAll

func DoAll(err error, f func(error))

DoAll iterates the passed function over all stacked or collected errors or simply the one that's passed.

func First

func First(existing, setting error) error

First checks the existing error for nil. If not it will be returned, otherwise the setting one. So usage is myStruct.err = failure.First(myStruct.err, newErr).

func IsValid

func IsValid(err error) bool

IsValid returns true if it is a valid error generated by this package.

func Location

func Location(err error) (string, error)

Location returns the package and the file name as well as the line number of the error.

func New

func New(msg string, args ...interface{}) error

New creates an error with the given code.

func Stack

func Stack(err error) []error

Stack returns a slice of errors down to the lowest not annotated error.

Types

type Error

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

Error encapsulates errors in a synchronized way.

func (*Error) Get

func (e *Error) Get() error

Get retrieves the encapsulated error.

func (*Error) IsNil

func (e *Error) IsNil() bool

IsNil checks is the encapsulated error is nil.

func (*Error) Set

func (e *Error) Set(err error)

Set sets the encapsulated error.

Jump to

Keyboard shortcuts

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