-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Handle aggregates correctly in
trail.ToGRPC
(#98)
Handle aggregates (and `os.IsNotExist`) in conversion to status errors. Aggregates now implement `Unwrap() []error`, the same method provided by `errors.Join`. This makes it unnecessary to directly implement As() and Is(), since the corresponding errors methods can use the sliced-Unwrap to traverse the errors. Both trail and IsNotFound now check for Unwrap() []error in their loops, making them correctly support aggregates (and std-joined errors).
- Loading branch information
1 parent
1cff453
commit 6b0832f
Showing
6 changed files
with
133 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package internal | ||
|
||
// TraverseErr traverses the err error chain until fn returns true. | ||
// Traversal stops on nil errors, fn(nil) is never called. | ||
// Returns true if fn matched, false otherwise. | ||
func TraverseErr(err error, fn func(error) (ok bool)) (ok bool) { | ||
if err == nil { | ||
return false | ||
} | ||
|
||
if fn(err) { | ||
return true | ||
} | ||
|
||
switch err := err.(type) { | ||
case interface{ Unwrap() error }: | ||
return TraverseErr(err.Unwrap(), fn) | ||
|
||
case interface{ Unwrap() []error }: | ||
for _, err2 := range err.Unwrap() { | ||
if TraverseErr(err2, fn) { | ||
return true | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} |
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