-
Notifications
You must be signed in to change notification settings - Fork 39
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
add BuildChain, for associating two errors together #6
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -335,3 +335,29 @@ func Find(origErr error, test func(error) bool) error { | |
}) | ||
return foundErr | ||
} | ||
|
||
// Chain ties together two errors, giving them a Causer interface and a concatenated Error message. | ||
type chain struct { | ||
currentErr error | ||
prevErr error | ||
} | ||
|
||
func (err *chain) Error() string { | ||
return err.currentErr.Error() + "\n" + err.prevErr.Error() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error should not got preErr. e.g. TruncateErr, error info should only contain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, that makes sense since this is joining two separate errors. The previous will show up in the formatter. |
||
} | ||
|
||
func (err *chain) Cause() error { | ||
return err.prevErr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we recursively find the root cause? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No: this is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hi @gregwebs , I check juju's impl again. It seem will return expect this two question, others are look good to me |
||
} | ||
|
||
// BuildChain constructs a Chain. | ||
// If the newer error is already a Chain, it will return the original ErrorChain | ||
// modified so that the previous error is a chain. | ||
func BuildChain(currentErr error, prevErr error) error { | ||
if currentChain, ok := currentErr.(*chain); ok { | ||
middleErr := currentChain.prevErr | ||
currentChain.prevErr = &chain{middleErr, prevErr} | ||
return currentChain | ||
} | ||
return &chain{currentErr, prevErr} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need override
ErrorStack
or%+v
to print errors info before wrap recursively.e.g.
should output:
I think
time: invalid duration dfasdf
is what we need for this methodThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, the formatter is added.