-
Notifications
You must be signed in to change notification settings - Fork 0
feat: filter reported errors #70
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package errorsfilter | ||
|
|
||
| import ( | ||
| "github.com/hashicorp/go-multierror" | ||
| "github.com/sdsc-ordes/quitsh/pkg/common/stack" | ||
| "github.com/sdsc-ordes/quitsh/pkg/log" | ||
| ) | ||
|
|
||
| // alreadyReportedErr is an wrapping error which can be dropped by `DropReported`. | ||
| type alreadyReportedErr struct { | ||
| e error | ||
| } | ||
|
|
||
| func (e *alreadyReportedErr) Error() string { | ||
| return e.e.Error() | ||
| } | ||
|
|
||
| func (e *alreadyReportedErr) Unwrap() error { | ||
| return e.e | ||
| } | ||
|
|
||
| // WrapAsReported returns a new wrapped errors which indicates | ||
| // that it is already reported. | ||
| // Useful for bigger errors, which you want to propagate upwards but | ||
| // maybe ignore in logging with [FilterAlreadyReported]. | ||
| func WrapAsReported(e error) error { | ||
| return &alreadyReportedErr{e: e} | ||
| } | ||
|
|
||
| // FilterAlreadyReported filters `ErrAlreadyReported` from the chain. | ||
| // NOTE: This only filters on [multierror.Error] s since, `Unwrap` functionality does not let | ||
| // reconstruct the error. We are using [multierror.Error] extensively anyways. | ||
| // | ||
| //nolint:gocognit,nestif // TODO: later. | ||
| func FilterAlreadyReported(err error) error { | ||
| if err == nil { | ||
| return nil | ||
| } | ||
|
|
||
| type Node struct { | ||
| parent *Node | ||
| e error | ||
|
|
||
| multi *multierror.Error | ||
| new error | ||
| visited bool // Denotes if we visited the node on down traversal. | ||
| } | ||
|
|
||
| stack := stack.NewStack[*Node]() | ||
| root := &Node{e: err} | ||
| stack.Push(root) | ||
|
|
||
| for stack.Len() != 0 { | ||
| node := stack.Top() | ||
| if node.visited { | ||
| // Backtracking | ||
| // Reconstruct errors. | ||
| ignore := false | ||
| if _, ok := node.e.(*alreadyReportedErr); ok { //nolint:errorlint // This is ok. | ||
| ignore = true | ||
| } | ||
|
|
||
| if !ignore { | ||
| // Create our new error from any children we have. | ||
| if node.multi != nil { | ||
| node.new = node.multi | ||
| } else { | ||
| node.new = node.e | ||
| } | ||
|
|
||
| // Add ourself to parent. | ||
| if node.parent != nil { | ||
| node.parent.multi.Errors = append(node.parent.multi.Errors, node.new) | ||
| } | ||
| } else { | ||
| log.Debug("Dropping error 'alreadyReportedErr'.") | ||
| } | ||
|
|
||
| stack.Pop() | ||
|
|
||
| continue | ||
| } | ||
|
|
||
| node.visited = true | ||
| if e, ok := node.e.(*multierror.Error); ok { //nolint:errorlint // This is ok. | ||
| node.multi = &multierror.Error{} // init a new multi list error | ||
| for _, c := range e.Errors { | ||
| stack.Push(&Node{e: c, parent: node}) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return root.new | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package errorsfilter | ||
|
|
||
| import ( | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/go-multierror" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestDropReported(t *testing.T) { | ||
| var errA = errors.New("error A") | ||
| var errB = errors.New("error B") | ||
| var errC = errors.New("error C") | ||
| var errD = errors.New("error D") | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| input error | ||
| wantNil bool | ||
| wantErrs []error // errors expected to survive (checked via errors.Is) | ||
| wantDrop []error // errors expected to be dropped | ||
| }{ | ||
| { | ||
| name: "nil input", | ||
| input: nil, | ||
| wantNil: true, | ||
| }, | ||
| { | ||
| name: "single error passes through", | ||
| input: errA, | ||
| wantErrs: []error{errA}, | ||
| }, | ||
| { | ||
| name: "multierror without ErrReported passes through", | ||
| input: multierror.Append(nil, errA, errB), | ||
| wantErrs: []error{errA, errB}, | ||
| }, | ||
| { | ||
| name: "wrapped error is dropped", | ||
| input: WrapAsReported(errA), | ||
| wantNil: true, | ||
| }, | ||
| { | ||
| name: "flat: two dropped in multierror", | ||
| input: multierror.Append(nil, | ||
| WrapAsReported(errA), // dropped | ||
| errB, | ||
| WrapAsReported(multierror.Append(nil, errC)), // dropped | ||
| ), | ||
| wantErrs: []error{errB}, | ||
| wantDrop: []error{errA, errC}, | ||
| }, | ||
| { | ||
| name: "nested: two dropped in multierror", | ||
| input: &multierror.Error{ | ||
| Errors: []error{ | ||
| WrapAsReported(errA), | ||
| errB, | ||
| &multierror.Error{ | ||
| Errors: []error{ | ||
| WrapAsReported(errC), | ||
| errD, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| wantErrs: []error{errB, errD}, | ||
| wantDrop: []error{errA, errC}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := FilterAlreadyReported(tt.input) | ||
|
|
||
| if tt.wantNil { | ||
| require.NoError(t, result) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| require.Error(t, result) | ||
|
|
||
| for _, want := range tt.wantErrs { | ||
| require.ErrorIs(t, result, want) | ||
| } | ||
|
|
||
| for _, dropped := range tt.wantDrop { | ||
| require.NotErrorIs(t, result, dropped) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.