-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
joinederr package for unwrapping after errors.Join (#15)
- Loading branch information
Showing
6 changed files
with
180 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module github.com/mvndaai/ctxerr | ||
|
||
go 1.18 | ||
go 1.20 |
This file contains 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,54 @@ | ||
package joinederr | ||
|
||
type ErrorIterator interface { | ||
Next() error | ||
HasNext() bool | ||
} | ||
|
||
type depthFirstUnwrapper struct { | ||
next error | ||
nextParent []error | ||
} | ||
|
||
func (bfu *depthFirstUnwrapper) Next() error { | ||
if bfu.next == nil { | ||
return nil | ||
} | ||
|
||
// Split joined errors | ||
if x, ok := bfu.next.(interface{ Unwrap() []error }); ok { | ||
errs := x.Unwrap() | ||
if len(errs) > 0 { | ||
bfu.next = errs[0] | ||
bfu.nextParent = append(errs[1:], bfu.nextParent...) | ||
} | ||
} | ||
|
||
// Set return value | ||
r := bfu.next | ||
|
||
// Setup next return value | ||
bfu.next = nil | ||
|
||
if x, ok := r.(interface{ Unwrap() error }); ok { | ||
bfu.next = x.Unwrap() | ||
} | ||
|
||
if bfu.next == nil { | ||
if len(bfu.nextParent) > 0 { | ||
bfu.next = bfu.nextParent[0] | ||
bfu.nextParent = bfu.nextParent[1:] | ||
} | ||
} | ||
|
||
// Return | ||
return r | ||
} | ||
|
||
func (bfu *depthFirstUnwrapper) HasNext() bool { | ||
return bfu.next != nil | ||
} | ||
|
||
func NewDepthFirstIterator(err error) ErrorIterator { | ||
return &depthFirstUnwrapper{next: err} | ||
} |
This file contains 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,71 @@ | ||
package joinederr_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/mvndaai/ctxerr/joinederr" | ||
) | ||
|
||
func TestBreadthFirst(t *testing.T) { | ||
/* | ||
a | ||
/ \ | ||
/ \ | ||
b f | ||
/ \ | | ||
c e g | ||
| / \ | ||
d h i | ||
*/ | ||
|
||
i := errors.New("i") | ||
h := errors.New("h") | ||
g := fmt.Errorf("g\n%w", errors.Join(h, i)) | ||
f := fmt.Errorf("f\n%w", g) | ||
d := errors.New("d") | ||
c := fmt.Errorf("c\n%w", d) | ||
e := errors.New("e") | ||
b := fmt.Errorf("b\n%w", errors.Join(c, e)) | ||
a := fmt.Errorf("a\n%w", errors.Join(b, f)) | ||
|
||
msgs := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i"} | ||
actualMsg := a.Error() | ||
expectedMessage := strings.Join(msgs, "\n") | ||
if actualMsg != expectedMessage { | ||
t.Errorf("message did not match\n%#v\n%#v", actualMsg, expectedMessage) | ||
} | ||
|
||
expectedTrees := [][]string{ | ||
{"a", "b", "c", "d", "e", "f", "g", "h", "i"}, | ||
{"b", "c", "d", "e"}, | ||
{"c", "d"}, | ||
{"d"}, | ||
{"e"}, | ||
{"f", "g", "h", "i"}, | ||
{"g", "h", "i"}, | ||
{"h"}, | ||
{"i"}, | ||
} | ||
var expectTreeCount int | ||
|
||
iter := joinederr.NewDepthFirstIterator(a) | ||
for iter.HasNext() { | ||
actualMsg := strings.ReplaceAll(iter.Next().Error(), "\n", ":") | ||
expectedMessage := strings.Join(expectedTrees[expectTreeCount], ":") | ||
if actualMsg != expectedMessage { | ||
t.Errorf("message [%v] did not match\n%#v\n%#v", expectTreeCount, actualMsg, expectedMessage) | ||
} | ||
expectTreeCount++ | ||
} | ||
|
||
if expectTreeCount != len(expectedTrees) { | ||
t.Errorf("stopped iterating [%v] setps early", len(expectedTrees)-expectTreeCount) | ||
} | ||
|
||
if iter.Next() != nil { | ||
t.Error("this there should be nothing left") | ||
} | ||
} |