errors

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2025 License: ISC Imports: 5 Imported by: 0

Documentation

Overview

Package errors provides custom error types and functionalities to enhance error handling.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultErrorFormat formats a slice of errors into a single string,
	// with each error on a new line.
	DefaultErrorFormat = defaultErrorFormat

	// BulletErrorFormat formats a slice of errors into a single string,
	// with each error indented, prefixed by a bullet point, and properly spaced.
	BulletErrorFormat = bulletErrorFormat
)

Functions

This section is empty.

Types

type ErrorFormat

type ErrorFormat func([]error) string

ErrorFormat is a function type for formatting a slice of errors into a single string representation.

type Group

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

Group is used for aggregating multiple errors from multiple goroutines. It provides thread-safe methods to run functions concurrently and collect any errors they return.

func (*Group) Go

func (e *Group) Go(f func() error)

Go starts a new goroutine to execute the given function f. If f returns an error, it will be safely added to the aggregated errors.

func (*Group) Wait

func (e *Group) Wait() *MultiError

Wait blocks until all goroutines started with the Go method have finished. It returns the aggregated errors from all goroutines. If no errors were returned, it returns nil.

type MultiError

type MultiError struct {
	Format ErrorFormat
	// contains filtered or unexported fields
}

MultiError represents an error type that aggregates multiple errors. It is used to accumulate and manage multiple errors, wrapping them into a single error instance.

func Append

func Append(e error, errs ...error) *MultiError

Append adds one or more errors to an existing error and returns a MultiError instance.

  • If the input error e is nil, a new instance of MultiError is created.
  • If e is not already an instance of MultiError, it will be wrapped into a new MultiError instance.
  • If any of the provided errors (errs) are instances of MultiError, they are flattened into the result.
Example
package main

import (
	"fmt"

	"github.com/moorara/algo/errors"
)

func main() {
	var err error
	err = errors.Append(err, fmt.Errorf("error on foo: %d", 1))
	err = errors.Append(err, fmt.Errorf("error on bar: %d", 2))
	err = errors.Append(err, fmt.Errorf("error on baz: %d", 3))

	fmt.Println(err)
}
Example (WithCustomFormat)
package main

import (
	"fmt"

	"github.com/moorara/algo/errors"
)

func main() {
	err := &errors.MultiError{
		Format: errors.BulletErrorFormat,
	}

	err = errors.Append(err, fmt.Errorf("error on foo: %d", 1))
	err = errors.Append(err, fmt.Errorf("error on bar: %d", 2))
	err = errors.Append(err, fmt.Errorf("error on baz: %d", 3))

	fmt.Println(err)
}

func (*MultiError) As

func (e *MultiError) As(target any) bool

As finds the first error in the MultiError's errors that matches target, and if one is found, sets target to that error value and returns true. Otherwise, it returns false.

func (*MultiError) Error

func (e *MultiError) Error() string

Error implements the error interface for MultiError. It formats the accumulated errors into a single string representation. If a custom ErrorFormat function is provided, it will be used; otherwise, the default ErrorFormat (DefaultErrorFormat) will be applied.

func (*MultiError) ErrorOrNil

func (e *MultiError) ErrorOrNil() error

// ErrorOrNil returns an error if the MultiError instance contains any errors, or nil if it has none. This method is useful for ensuring that a valid error value is returned after accumulating errors, indicating whether errors are present or not.

Example
package main

import (
	"fmt"

	"github.com/moorara/algo/errors"
)

func main() {
	me := new(errors.MultiError)
	err := me.ErrorOrNil()

	fmt.Println(err)
}

func (*MultiError) Is

func (e *MultiError) Is(target error) bool

Is checks if any of the errors in the MultiError matches the target error.

func (*MultiError) Unwrap

func (e *MultiError) Unwrap() []error

Unwrap implements the unwrap interface for MultiError. It returns the slice of accumulated errors wrapped in the MultiError instance. If there are no errors, it returns nil, indicating that e does not wrap any error.

Example
package main

import (
	"fmt"

	"github.com/moorara/algo/errors"
)

func main() {
	var me *errors.MultiError
	me = errors.Append(me, fmt.Errorf("error on foo: %d", 1))
	me = errors.Append(me, fmt.Errorf("error on bar: %d", 2))
	me = errors.Append(me, fmt.Errorf("error on baz: %d", 3))

	for _, err := range me.Unwrap() {
		fmt.Println(err)
	}
}

Jump to

Keyboard shortcuts

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