Categorygithub.com/mrflynn/go-joinederror
modulepackage
0.2.0
Repository: https://github.com/mrflynn/go-joinederror.git
Documentation: pkg.go.dev

# README

go-joinederror

Tests

Small library for unpacking errors into a slice that were combined using the errors.Join() function in Go 1.20.

Install

$ go get github.com/mrflynn/go-joinederror

Usage

This library provides two ways to unpack joined errors. UnwrapMany unpacks the top level joined errors, and UnwrapAll recursively unpacks all joined errors at every level. For example,

// Create a joined error.
err := errors.Join(
	errors.New("lorem"),
	errors.New("ipsum"),
	errors.Join(errors.New("dolor"), errors.New("sit")),
)

// UnwrapMany:
comparison := []error{
	errors.New("lorem"),
	errors.New("ipsum"),
	errors.Join(errors.New("dolor"), errors.New("sit"))
}

fmt.Println(reflect.DeepEqual(joinederror.UnwrapMany(err), comparison)) // Prints true

// UnwrapAll:
comparison := []error{
	errors.New("lorem"),
	errors.New("ipsum"),
	errors.New("dolor"),
	errors.New("sit"),
}

fmt.Println(reflect.DeepEqual(joinederror.UnwrapAll(err), comparison)) // Prints true

# Functions

UnwrapAll is similar to UnwrapMany except it unwraps joined errors recursively.
UnwrapMany takes a joined error from errors.Join and returns the slice of errors that make up the joined error.