import "github.com/eurielec/bulkerrs"This package provides a simple and effective way to collect errors while simplifies the flow complexity of sequential and conditional safety checks.
The exported 'NewErr' and 'NewErrOr' alows initialization with or without a previous 'Errs'. Theses functions work like a wrap around the built-in and 'juju/errors' constructor functions.
Once initialized, 'errs.NewErr' and 'errs.NewErrWithCause' work as a replacement of 'errors.NewErr' and 'errors.NewErrWithCause' that appends the generated jujuErr to the inner errors. Also the function 'errs.Append' appends the submitted error arguments to the inner slice.
A primary use case for this library is to append multiple errors while doing a sequence of checkings.
errs := make([]error, 0)
if err := checkFunc1(); err != nil {
errs = append(errs, err)
}
if err := checkFunc2(); err != nil {
errs = append(errs, err)
}
Would become with github.com/juju/errors:
errs := make([]error, 0)
if err := checkFunc1(); err != nil {
errs = append(errs, errors.Trace(err))
}
if err := checkFunc2(); err != nil {
errs = append(errs, errors.Annotate(err, "more context"))
// Adding annotation to the error
}
And with bulkerrs:
errs := bulkerrs.NewErrOr(checkFunc1())
errs.NewErrWithCause(checkFunc2(), "more context")
There's no longer need to check if the error is nil.
Additionally, bulkerrs makes easy to integrate the errors appendings in the application control flow:
errs := make([]error, 0)
if err1 := checkFunc1(); err1 != nil {
errs = append(errs, err1)
if err1_1 := checkFunc1_1(); err1_1 != nil {
errs = append(errs, err1_1)
}
} else if err2 := checkFunc2(); err2 != nil {
errs = append(errs, err2)
if err2_1 := checkFunc2_1(); err2_1 != nil {
errs = append(errs, err2_1)
}
} else {
if errx_1 := checkFuncx(); errx_1 != nil {
errs = append(errs, errx_1)
} else {
return nil
}
}
return &errs
Would become:
errs := bulkerrs.NewErr()
if errs.Append(checkFunc1()) {
errs.Append(checkFunc1_1())
} else if errs.Append(checkFunc2()) {
errs.Append(checkFunc2_1())
} else {
errs.Append(checkFuncx())
}
// return an error if len(errs.errors) > 0 or nil
return errs.ToError()
And if needed, like in github.com/juju/errors, it's possible to add extra context, and have an advanced control of the application flow:
errs := bulkerrs.NewErr()
if err1 := checkFunc1(); errs.AppendIfX(err1 != nil, errors.Annotate, "more context1", err1) {
errs.Append(checkFunc1_1())
} else if err2 := checkFunc2(); errs.AppendIfX(err2 == nil, errors.Annotate, "This should have failed but didn't", nil) {
errs.Append(checkFunc2_1())
} else {
errx := checkFuncx()
// add extra error types
errs.AppendIfX(errx != nil, errors.NewNotValid, "more context", errx)
}
return errs.ToError()
- func IsAlreadyExists(err error) bool
- func IsBadRequest(err error) bool
- func IsForbidden(err error) bool
- func IsMethodNotAllowed(err error) bool
- func IsNotAssigned(err error) bool
- func IsNotFound(err error) bool
- func IsNotImplemented(err error) bool
- func IsNotProvisioned(err error) bool
- func IsNotSupported(err error) bool
- func IsNotValid(err error) bool
- func IsTimeout(err error) bool
- func IsUnauthorized(err error) bool
- func IsUserNotFound(err error) bool
- type Errs
- func Concat(errs ...error) Errs
- func NewErr() Errs
- func NewErrOr(err error) Errs
- func (e *Errs) Append(errs ...error) bool
- func (e *Errs) AppendIf(condition bool, msg string) bool
- func (e *Errs) AppendIfX(condition bool, newErr NewXFn, msg string, other error) bool
- func (e *Errs) Error() string
- func (e *Errs) Errors() []string
- func (e Errs) Format(s fmt.State, verb rune)
- func (e *Errs) InnerErrors() []error
- func (e *Errs) NewErr(format string, args ...interface{})
- func (e *Errs) NewErrWithCause(other error, format string, args ...interface{})
- func (e Errs) ToError() error
- type IsXFn
- type JujuErr
- type NewXFn
func IsAlreadyExists
func IsAlreadyExists(err error) boolfunc IsBadRequest
func IsBadRequest(err error) boolfunc IsForbidden
func IsForbidden(err error) boolfunc IsMethodNotAllowed
func IsMethodNotAllowed(err error) boolfunc IsNotAssigned
func IsNotAssigned(err error) boolfunc IsNotFound
func IsNotFound(err error) boolfunc IsNotImplemented
func IsNotImplemented(err error) boolfunc IsNotProvisioned
func IsNotProvisioned(err error) boolfunc IsNotSupported
func IsNotSupported(err error) boolfunc IsNotValid
func IsNotValid(err error) boolfunc IsTimeout
func IsTimeout(err error) boolfunc IsUnauthorized
func IsUnauthorized(err error) boolfunc IsUserNotFound
func IsUserNotFound(err error) booltype Errs
Errs holds an array of JujuErr (juju/errors).
It may be embedded in also custom error types that have been converted to JujuErr.
type Errs struct {
// contains filtered or unexported fields
}func Concat
func Concat(errs ...error) Errsfunc NewErr
func NewErr() Errsfunc NewErrOr
func NewErrOr(err error) Errsfunc (*Errs) Append
func (e *Errs) Append(errs ...error) boolfunc (*Errs) AppendIf
func (e *Errs) AppendIf(condition bool, msg string) boolAppends error if condition, returns condition
func (*Errs) AppendIfX
func (e *Errs) AppendIfX(condition bool, newErr NewXFn, msg string, other error) boolfunc (*Errs) Error
func (e *Errs) Error() stringfunc (*Errs) Errors
func (e *Errs) Errors() []stringfunc (Errs) Format
func (e Errs) Format(s fmt.State, verb rune)func (*Errs) InnerErrors
func (e *Errs) InnerErrors() []errorfunc (*Errs) NewErr
func (e *Errs) NewErr(format string, args ...interface{})func (*Errs) NewErrWithCause
func (e *Errs) NewErrWithCause(other error, format string, args ...interface{})func (Errs) ToError
func (e Errs) ToError() errortype IsXFn
type IsXFn func(err error) booltype JujuErr
If an error complies this interface, then it's a juju error :)
type JujuErr interface {
Cause() error
Error() string
Format(fmt.State, rune)
Location() (string, int)
Message() string
SetLocation(int)
StackTrace() []string
Underlying() error
}type NewXFn
Juju Error function interfaces
type NewXFn func(error, string) errorGenerated by gomarkdoc