Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterate over nested causes #54

Open
chudley opened this issue May 31, 2018 · 2 comments
Open

Iterate over nested causes #54

chudley opened this issue May 31, 2018 · 2 comments

Comments

@chudley
Copy link

chudley commented May 31, 2018

VError's cause mechanism is extremely useful for keeping track of an error's potentially nested reason for existing. It's possible to explore this nested structure with the VError.errorForEach() method, which will take the appropriate action depending on the cause (i.e. loop over each error in a MultiError, return the single error if it's a VError, etc.)

It would be great if node-verror could provide a mechanism for iteratively exploring a nested VError so that a consumer can exhaustively display each of the causes of a VError chain in a way that suits them.

A use case I've been working on recently is to have a CLI tool print a human-friendly message to the terminal in the event of failure, where the cause of the failure is a nested set of VErrors and/or MultiErrors. Having the CLI tool print the top-level error would mask much of the detail below if any of the causes are a MultiError (the message would contain the "first of 3 errors: ..." summary), but all of it is useful.

@chudley
Copy link
Author

chudley commented May 31, 2018

I've created an example in this gist which can be dropped into the "experiments/" folder of a working node-verror project to try out. Note that it's making use of WError to prevent repeated "first of 3 errors: ..." messages on parent-level VErrors, which is potentially a stretch given WErrors documented use case.

I wanted to ping it here for early discussion (in lieu of writing tests) before heading to Gerrit because there's the potential to have the existing VError.errorForEach() method simply make use of this new VError.errorForEachIter() function with a depth of 1, or possibly even have VError.errorForEach() do the iteration itself but default to 1 for backwards compatibility.

@jcheroske
Copy link

I've just run into this exact thing. I can implement the recursion myself, but it would be super cool if the library had a tree traversal function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants