merror

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: May 29, 2023 License: MIT Imports: 4 Imported by: 39

README

merror

GoDoc Build Status

Multiple Error aggregator for Go.

Usage

func foo() error {
  merr := merror.New()

  if err := DoSomething(); err != nil {
    merr.Append(err)
  }

  return merr.ErrorOrNil()
}

A bounded merror can be used to guard against memory ballooning.

func bar() error {
  merr := merror.NewWithCap(10)

  for i := 0; i < 15; i++ {
    if err := DoSomething(); err != nil {
      merr.Append(err)
    }
  }

  fmt.Printf("Len: %d,  Overflow: %d", merr.Len(), merr.Overflow()) 
  // Len: 10,  Overflow: 5

  return merr.ErrorOrNil()
}

errors.Is

If any of the errors appended to a merror match the target error passed to errors.Is(err, target error) then true is returned.

errors.As

If any of the errors appended to a merror match the target type passed to errors.As(err error, target any) then true is returned and the target is set to the matching error.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GlobalFormatter = defaultFormatter

GlobalFormatter is the global merror formatter. Set this to a custom formatter if desired.

Functions

func Append added in v1.0.4

func Append(to, err error) error

Append an error to a multi-error. If `to` is `nil` it will just assign `err`. If `to` is not a `*MError` it will create a new `*MError` and append both errors. If `err` is `nil` it will just return `to`. Otherwise it will just append to the existing `*MError`.

Types

type FormatterFunc

type FormatterFunc func(merr *MError) string

FormatterFunc is a function that converts a merror to a string.

type MError

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

MError represents zero or more errors that can be accumulated via the `Append` method.

func New

func New() *MError

New returns a new instance of `MError` with no limit on the number of errors that can be appended.

func NewWithCap

func NewWithCap(cap int) *MError

NewWithCap returns a new instance of `MError` with a maximum capacity of `cap` errors. If exceeded only the overflow counter will be incremented.

A `cap` of zero of less means no cap and max size of a slice on the current platform is the upper bound.

func (*MError) Append

func (me *MError) Append(errs ...error)

Append adds an error to the aggregated error list.

func (*MError) As added in v1.0.5

func (me *MError) As(target interface{}) bool

func (*MError) Error

func (me *MError) Error() string

Error returns a string representation of this MError. The output format depends on the `Formatter` set for this merror instance, or the global formatter if none set.

func (*MError) ErrorOrNil

func (me *MError) ErrorOrNil() error

ErrorOrNil returns nil if this `MError` contains no errors, otherwise this `MError` is returned.

func (*MError) Errors

func (me *MError) Errors() []error

Errors returns a slice of the `error` instances that have been appended to this `MError`.

func (*MError) Is added in v1.0.5

func (me *MError) Is(target error) bool

func (*MError) Len

func (me *MError) Len() int

Len returns the number of errors that have been appended.

func (*MError) Overflow

func (me *MError) Overflow() int

Overflow returns the number of errors that have been truncated because maximum capacity was exceeded.

func (*MError) SetFormatter

func (me *MError) SetFormatter(f FormatterFunc) (old FormatterFunc)

SetFormatter sets the `FormatterFunc` to be used when `Error` is called. The previous `FormatterFunc` is returned.

Jump to

Keyboard shortcuts

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