errors

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: BSD-3-Clause Imports: 6 Imported by: 36

Documentation

Index

Examples

Constants

View Source
const (
	// SeverityUnset indicates the severity was not set
	SeverityUnset = Severity("")
	// SeverityRuntime indicates the error is returned for an operation that should/could be executed again. For example, timeouts.
	SeverityRuntime = Severity("runtime")
	// SeverityFatal indicates the error is unrecoverable and the execution should stop, or not being retried.
	SeverityFatal = Severity("fatal")
	// SeverityInput indicates  an expected, like a bad user input/request. For example, an invalid email.
	SeverityInput = Severity("input")
)
View Source
const (
	// CodeEmpty is the zero-value for error codes
	CodeEmpty = Code("")
)

Variables

This section is empty.

Functions

func ConcatErrors

func ConcatErrors(errs ...error) error

ConcatErrors returns an error with a message that is the concatenation of all messages of the given errors

func ConcatErrorsMessage

func ConcatErrorsMessage(errs ...error) string

ConcatErrorsMessage concatenates all the error messages from the given errors

func DontPanic

func DontPanic(f func()) (err error)

DontPanic executes f and, if f panics, recovers from the panic and returns the panic wrapped as an Error.

func E

func E(args ...interface{}) error

E is a helper function for building errors.

If called with no arguments, it returns an error solely containing a message informing that.

This method can be called with any set of parameters of any type, but it requires either an error or a string at least, otherwise it will return nil.

Parameters can be passed in any order and even be of repeated types, although only the last value of each type will be considered, except for KeyValue and []KeyValue, which will be concatenated to the struct's KVs value.

Types other than string, Code, Severity, error, Op, KeyValue, or []KeyValue will simply be ignored.

Example
// Calling E with no arguments results in an error with a message saying
// that the function was called with no arguments
errNoArgs := E()
fmt.Println(errNoArgs)

// Calling E without a string or an error will result in a nil return
errNil := E(Op("Error Example"), Code("ERROR_EXAMPLE_NIL"))
fmt.Println(errNil)

// E requires either a string or an err to return an error
withString := E("This string will be used to build an error")
previous := errors.New("Previous error")
withError := E(previous)
fmt.Println(withString, withError)

// We can pass parameters of the same type more than once, but only the last
// one will be considered
multiOp := E("Multi op", Op("Op 1"), Op("Op 2"), Op("Op 3"))
fmt.Println(multiOp)

// Except for KeyValue and []KeyValue which will be concatenated
kv := KeyValue{Key: "key1", Value: "val1"}
multiKv := E("Multi kv", kv, kv, kv)
fmt.Println(multiKv)

// Values of unexpected types will be ignored
intErr := E("Int err", 1, 2, 3)
fmt.Println(intErr)

// Full example
op := Op("errors.errorExample")
code := Code("ERROR_EXAMPLE")
sev := SeverityRuntime
kv = KeyValue{Key: "key", Value: "val"}

err := E("Error example", op, code, sev, kv)
fmt.Println(err)
Output:

func EqualsCode

func EqualsCode(lCode, rCode Code) bool

EqualsCode returns true if @lCode and @rCode holds the same value, and false otherwise

func Errorf

func Errorf(s string, params ...interface{}) error

Errorf returns a error based on given params. It is a wrap of the fmt.Errorf method that returns nil when given an empty string

func GetRootError

func GetRootError(err error) error

GetRootError returns the deepest error in the Err stack. That is, while an Error has a previous Error, keep getting the previous and returns when previous no longer has an Err

func GetRootErrorWithKV

func GetRootErrorWithKV(err error) error

GetRootErrorWithKV returns the Err field of Error struct or the error itself if it is of another type

func New

func New(s string) error

New returns a new error. It is a wrap of Go's errors.New method that when given an empty string, returns nil

func SameCode

func SameCode(lError, rError error) bool

SameCode returns true if @lError and @rError holds error codes with the same value, and false otherwise. If one or both errors have no code, SameCode will return false.

Types

type Code

type Code string

Code is the error code

var CodePanic Code = "PANIC"

CodePanic represents the error code for panic

func GetCode

func GetCode(err error) Code

GetCode returns the error code. If the error doesn't contains an error code, returns ErrorCodeEmpty

func (Code) MarshalZerologObject

func (c Code) MarshalZerologObject(e *zerolog.Event)

MarshalZerologObject allows for zerolog to log the error code as 'error_code': '...'

func (Code) String

func (c Code) String() string

String returns the code as a string

type Error

type Error struct {
	// Severity contains information about the nature of the error so you can
	// decide what to do
	Severity Severity
	// Err is the previous error, usually given by a lower level layer to be
	// wrapped
	Err error
	// Code is an application friendly way to describe the error
	Code Code
	// Op is the operation that was happening when the error occurred. In other
	// words, is where the error happened
	Op Op
	// KVs is a map os values you can use to enrich your error with relevant
	// information
	KVs []KeyValue
}

Error is the error struct that should be returned in all functions. It is ready to hold useful data about the error and has methods that make building and extracting information very easy. It also implements the Go's error interface

func NewFromRecover

func NewFromRecover(r interface{}) Error

NewFromRecover returns a new Error created from the result of a recover. If r is an Error, this will be used so Op and KV are preserved

func (Error) Error

func (e Error) Error() string

Error formats the error information into a string. By implementing this method, we implement Go's error interface

func (Error) String

func (e Error) String() string

String is required to implement the stringer interface

func (Error) Unwrap added in v0.3.0

func (e Error) Unwrap() error

Unwrap returns the previous error

type KeyValue

type KeyValue struct {
	Key   interface{}
	Value interface{}
}

KeyValue is used to store a key-value pair within the error

func KV

func KV(k, v interface{}) KeyValue

KV is a constructor for KeyValue types

func (KeyValue) String

func (kv KeyValue) String() string

type Op

type Op string

Op is the operation that encapsulated the error

func (Op) String

func (o Op) String() string

type Severity

type Severity string

Severity is the error severity. It's used to classify errors in groups to be easily handled by the code. For example, a retry layer should be only checking for Runtime errors to retry. Or in an HTTP layer, errors of input type are always returned a 400 status.

func GetSeverity

func GetSeverity(err error) Severity

GetSeverity returns the error severity. If there is not severity, SeverityUnset is returned.

func (Severity) MarshalZerologObject

func (s Severity) MarshalZerologObject(e *zerolog.Event)

MarshalZerologObject allows for zerolog to log the error severity as 'error_severity': '...'

func (Severity) String

func (s Severity) String() string

Jump to

Keyboard shortcuts

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