项目作者: Eurielec

项目描述 :
github.com/juju/errors in bulk
高级语言: Go
项目地址: git://github.com/Eurielec/bulkerrs.git
创建时间: 2020-03-13T18:29:49Z
项目社区:https://github.com/Eurielec/bulkerrs

开源协议:GNU General Public License v3.0

下载


Go Report Card
CircleCI

bulkerrs

  1. 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.

  1. errs := make([]error, 0)
  2. if err := checkFunc1(); err != nil {
  3. errs = append(errs, err)
  4. }
  5. if err := checkFunc2(); err != nil {
  6. errs = append(errs, err)
  7. }

Would become with github.com/juju/errors:

  1. errs := make([]error, 0)
  2. if err := checkFunc1(); err != nil {
  3. errs = append(errs, errors.Trace(err))
  4. }
  5. if err := checkFunc2(); err != nil {
  6. errs = append(errs, errors.Annotate(err, "more context"))
  7. // Adding annotation to the error
  8. }

And with bulkerrs:

  1. errs := bulkerrs.NewErrOr(checkFunc1())
  2. 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:

  1. errs := make([]error, 0)
  2. if err1 := checkFunc1(); err1 != nil {
  3. errs = append(errs, err1)
  4. if err1_1 := checkFunc1_1(); err1_1 != nil {
  5. errs = append(errs, err1_1)
  6. }
  7. } else if err2 := checkFunc2(); err2 != nil {
  8. errs = append(errs, err2)
  9. if err2_1 := checkFunc2_1(); err2_1 != nil {
  10. errs = append(errs, err2_1)
  11. }
  12. } else {
  13. if errx_1 := checkFuncx(); errx_1 != nil {
  14. errs = append(errs, errx_1)
  15. } else {
  16. return nil
  17. }
  18. }
  19. return &errs

Would become:

  1. errs := bulkerrs.NewErr()
  2. if errs.Append(checkFunc1()) {
  3. errs.Append(checkFunc1_1())
  4. } else if errs.Append(checkFunc2()) {
  5. errs.Append(checkFunc2_1())
  6. } else {
  7. errs.Append(checkFuncx())
  8. }
  9. // return an error if len(errs.errors) > 0 or nil
  10. 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:

  1. errs := bulkerrs.NewErr()
  2. if err1 := checkFunc1(); errs.AppendIfX(err1 != nil, errors.Annotate, "more context1", err1) {
  3. errs.Append(checkFunc1_1())
  4. } else if err2 := checkFunc2(); errs.AppendIfX(err2 == nil, errors.Annotate, "This should have failed but didn't", nil) {
  5. errs.Append(checkFunc2_1())
  6. } else {
  7. errx := checkFuncx()
  8. // add extra error types
  9. errs.AppendIfX(errx != nil, errors.NewNotValid, "more context", errx)
  10. }
  11. return errs.ToError()

Index

func IsAlreadyExists

  1. func IsAlreadyExists(err error) bool

func IsBadRequest

  1. func IsBadRequest(err error) bool

func IsForbidden

  1. func IsForbidden(err error) bool

func IsMethodNotAllowed

  1. func IsMethodNotAllowed(err error) bool

func IsNotAssigned

  1. func IsNotAssigned(err error) bool

func IsNotFound

  1. func IsNotFound(err error) bool

func IsNotImplemented

  1. func IsNotImplemented(err error) bool

func IsNotProvisioned

  1. func IsNotProvisioned(err error) bool

func IsNotSupported

  1. func IsNotSupported(err error) bool

func IsNotValid

  1. func IsNotValid(err error) bool

func IsTimeout

  1. func IsTimeout(err error) bool

func IsUnauthorized

  1. func IsUnauthorized(err error) bool

func IsUserNotFound

  1. func IsUserNotFound(err error) bool

type Errs

Errs holds an array of JujuErr (juju/errors).

It may be embedded in also custom error types that have been converted to JujuErr.

  1. type Errs struct {
  2. // contains filtered or unexported fields
  3. }

func Concat

  1. func Concat(errs ...error) Errs

func NewErr

  1. func NewErr() Errs

func NewErrOr

  1. func NewErrOr(err error) Errs

func (*Errs) Append

  1. func (e *Errs) Append(errs ...error) bool

func (*Errs) AppendIf

  1. func (e *Errs) AppendIf(condition bool, msg string) bool

Appends error if condition\, returns condition

func (*Errs) AppendIfX

  1. func (e *Errs) AppendIfX(condition bool, newErr NewXFn, msg string, other error) bool

func (*Errs) Error

  1. func (e *Errs) Error() string

func (*Errs) Errors

  1. func (e *Errs) Errors() []string

func (Errs) Format

  1. func (e Errs) Format(s fmt.State, verb rune)

func (*Errs) InnerErrors

  1. func (e *Errs) InnerErrors() []error

func (*Errs) NewErr

  1. func (e *Errs) NewErr(format string, args ...interface{})

func (*Errs) NewErrWithCause

  1. func (e *Errs) NewErrWithCause(other error, format string, args ...interface{})

func (Errs) ToError

  1. func (e Errs) ToError() error

type IsXFn

  1. type IsXFn func(err error) bool

type JujuErr

If an error complies this interface\, then it’s a juju error :)

  1. type JujuErr interface {
  2. Cause() error
  3. Error() string
  4. Format(fmt.State, rune)
  5. Location() (string, int)
  6. Message() string
  7. SetLocation(int)
  8. StackTrace() []string
  9. Underlying() error
  10. }

type NewXFn

Juju Error function interfaces

  1. type NewXFn func(error, string) error

Generated by gomarkdoc