errors

package module
v0.0.0-...-0de4fe6 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2018 License: MIT Imports: 5 Imported by: 0

README

errors

This package provides helpers to deal with error handling in go programs

Features

Error Slice
errs := errors.NewSlice()
errs = errs.Add(errors.New("err1"))
errs = errs.Add(errors.New("err2"))
errs = errors.MergeSlice(errs, errors.New("err3"), errors.Slice{errors.New("err4)})

Error Map
errs := errors.NewMap()
errs = errs.Add("field1", errors.New("err1"))
errs = errs.Add("field2", errors.New("err2"))
errs = errors.MergeMap(errs, errors.Map{"field3": errors.New("err3)})
Wrap, Wrapf, WithStack

There are wrappers for the most used functions from the github.com/pkg/errors package

Marshaling according to PROD-03

MarshalToJSON, MarshalToYAML and MarshalToObject are supported

errSlice := errors.Slice{errors.New("err1"),errors.New("err2")}
errMap := errors.Map{"f1": errors.New("err3")}
buf := &bytes.Buffer{}
errors.MarshalToJSON(buf, errSlice, errMap, errors.New("err4"))
fmt.Print(buf.String())
// prints: { "errors": ["err1", "err2", "err4"], "fieldErrors": {"f1": "err3"} }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsMap

func IsMap(err error) bool

IsMap checks if an error is a error map

func IsSlice

func IsSlice(err error) bool

IsSlice checks if an error is a errors.Slice

func MarshalToJSON

func MarshalToJSON(writer io.Writer, err error, additionalErrors ...error) error

MarshalToJSON an error to a json encoded error object

func MarshalToObject

func MarshalToObject(err error, additionalErrors ...error) map[string]interface{}

MarshalToObject marshals to a map[string]interface{}, usable to marshal to different formats

func MarshalToYAML

func MarshalToYAML(writer io.Writer, err error, additionalErrors ...error) error

MarshalToYAML marshals an error to a yaml encoded error object

func WithStack

func WithStack(err error) error

WithStack annotates the error with a stack trace

func Wrap

func Wrap(err error, message string) error

Wrap annotates the error with a message

func Wrapf

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

Wrapf annotates the errors with a formatted message

Types

type Error

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

Error is a marshalable error note that you loose any extra information other than the actual message when marshaling/unmarshaling

func New

func New(e interface{}) *Error

New creates a new marshalable error it may take a string or another error

func (*Error) Error

func (e *Error) Error() string

func (*Error) MarshalJSON

func (e *Error) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface

func (*Error) UnmarshalJSON

func (e *Error) UnmarshalJSON(bs []byte) error

UnmarshalJSON implements the json.Unmarshaler interface

type Map

type Map map[string]Slice

Map is a map from string to error

func MergeMap

func MergeMap(err1 Map, err2 Map) Map

MergeMap merges multiple error maps

func NewMap

func NewMap() Map

NewMap creates a new empty error map

func ToMap

func ToMap(err error, key ...string) Map

ToMap converts an error to a map error, key is optional and defaults to the empty string

func (Map) Add

func (m Map) Add(key string, err error) Map

Add sets an error in the map

func (Map) Error

func (m Map) Error() string

type Slice

type Slice []*Error

Slice is a slice of errors

func MergeSlice

func MergeSlice(err error, additionalErrors ...error) Slice

MergeSlice merges one or more errors to a slice

func NewSlice

func NewSlice() Slice

NewSlice creates a new empty errors slice

func ToSlice

func ToSlice(err error, additionalErrors ...error) Slice

ToSlice converts an error to a errors.Slice this function reminds me about the good old haskel days :D

func (Slice) Add

func (s Slice) Add(errs ...error) Slice

Add adds an error to the slice

func (Slice) Error

func (s Slice) Error() string

Jump to

Keyboard shortcuts

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