-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Raise PathUnsupported & PathInvalid errors
- Loading branch information
1 parent
a387089
commit d258674
Showing
16 changed files
with
345 additions
and
177 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
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,95 @@ | ||
package engine_errs | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type ( | ||
ErrorTree struct { | ||
Chain []string `json:"chain,omitempty"` | ||
Children []ErrorTree `json:"children,omitempty"` | ||
} | ||
|
||
chainErr interface { | ||
error | ||
Unwrap() error | ||
} | ||
joinErr interface { | ||
error | ||
Unwrap() []error | ||
} | ||
) | ||
|
||
func unwrapChain(err error) (chain []string, last joinErr) { | ||
for current := err; current != nil; { | ||
var next error | ||
cc, ok := current.(chainErr) | ||
if ok { | ||
next = cc.Unwrap() | ||
} else { | ||
joined, ok := current.(joinErr) | ||
if ok { | ||
jerrs := joined.Unwrap() | ||
if len(jerrs) == 1 { | ||
next = jerrs[0] | ||
} else { | ||
last = joined | ||
return | ||
} | ||
} else { | ||
chain = append(chain, current.Error()) | ||
return | ||
} | ||
} | ||
msg := strings.TrimSuffix(strings.TrimSuffix(current.Error(), next.Error()), ": ") | ||
if msg != "" { | ||
chain = append(chain, msg) | ||
} | ||
current = next | ||
} | ||
return | ||
} | ||
|
||
func ErrorsToTree(err error) (tree ErrorTree) { | ||
if err == nil { | ||
return | ||
} | ||
if t, ok := err.(ErrorTree); ok { | ||
return t | ||
} | ||
|
||
var joined joinErr | ||
tree.Chain, joined = unwrapChain(err) | ||
|
||
if joined != nil { | ||
errs := joined.Unwrap() | ||
tree.Children = make([]ErrorTree, len(errs)) | ||
for i, e := range errs { | ||
tree.Children[i] = ErrorsToTree(e) | ||
} | ||
} | ||
return | ||
} | ||
|
||
func (t ErrorTree) Error() string { | ||
sb := &strings.Builder{} | ||
t.print(sb, 0, 0) | ||
return sb.String() | ||
} | ||
|
||
func (t ErrorTree) print(out *strings.Builder, indent int, childChar rune) { | ||
prefix := strings.Repeat("\t", indent) | ||
delim := "" | ||
if childChar != 0 { | ||
delim = string(childChar) + " " | ||
} | ||
fmt.Fprintf(out, "%s%s%v\n", prefix, delim, t.Chain) | ||
for i, child := range t.Children { | ||
char := '├' | ||
if i == len(t.Children)-1 { | ||
char = '└' | ||
} | ||
child.print(out, indent+1, char) | ||
} | ||
} |
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
Oops, something went wrong.