Coder Social home page Coder Social logo

errortree's Introduction

errortree

Go Reference Go Report Card MIT License

Package errortree provides multiple-error matching considering the tree structure of errors in Go1.20 and later.

This package also provides the ability to retrieve errors from the tree, using generics to keep the type info from the caller, and returns the search result with an arbitrary concrete type.

Why created the std errors extension?

Standard errors will support tree structures starting with Go1.20. Its design is as follows:

  • Depth-first tree traversal
  • If the target matches, the traversal ends and the result is returned
  • Not all branches in the tree have to match

So it is difficult to satisfying some of the requirements!

Requirement A: Check all branches in the tree whether err and target are equal

For example, sometimes users want to ignore err or only logging for target errors, like:

switch err := ExecuteSomeFunc(); {
case errors.Is(err, TargetErr):
    // ignore or logging...
case err != nil:
    return err
}

This code checks for errors that can be ignored. In this case, if multiErr{targetErr, DifferentErr} or similar is passed as err, errors.Is(err, target) will return true even though it contains DifferentErr.

In some contexts, there will be use cases where user want to make sure that all branches match more strictly to report whether the err tree is equal to target. std error.Is() cannot be used for such a purpose.

So, created following functionlity for a more exact comparison.

ExactlyIs(err error, target error) bool

Requirement B: Extract all matching targets in the tree

The std errors.As() retrieves only the first error that matches the target, but there are some cases where the user may want to retrieve all matching errors for handling purposes.

For this reason, this package provides the following functionality to retrieve errors of a concrete type using generics.

Scan[T any](err error, targetT) []T

Example

ExactlyIs

erra := errors.New("error A")
err := multiErr{
	wrapErr{
		multiErr{
			erra,
			erra,
		},
	},
	wrapErr{erra},
	multiErr{
		erra,
		erra,
		errors.New("wrong error"),
	},
}
result := errortree.ExactlyIs(err, erra)
fmt.Println(result)

// Output:
// false

Scan

err := multiErr{
	multiErr{
		errors.New("error"),
		&fs.PathError{Op: "poser A"},
	},
	wrapErr{&fs.PathError{Op: "poser B"}},
	multiErr{
		errors.New("error"),
		errors.New("error"),
		wrapErr{&fs.PathError{Op: "poser C"}},
	},
}
var p *fs.PathError
matched := errortree.Scan(err, p)
for _, v := range matched {
	fmt.Printf("%s\n", v.Op)
}

// Output:
// poser A
// poser B
// poser C

errortree's People

Contributors

convto avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.