-
Notifications
You must be signed in to change notification settings - Fork 24
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
fix(errors): support error checking methods for new problem structures #212
Changes from all commits
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 |
---|---|---|
|
@@ -74,6 +74,13 @@ type IBMProblem struct { | |
// causedBy allows for the storage of a problem from a previous component, | ||
// if there is one. | ||
causedBy Problem | ||
|
||
// nativeCausedBy allows for the storage of an error that is the cause of | ||
// the problem instance but is not a part of the official chain of problem | ||
// types. By including these errors in the "Unwrap" chain, the problem type | ||
// changes become compatible with downstream code that uses error checking | ||
// methods like "Is" and "As". | ||
nativeCausedBy error | ||
} | ||
|
||
// Error returns the problem's message and implements the native | ||
|
@@ -109,12 +116,21 @@ func (e *IBMProblem) GetCausedBy() Problem { | |
// it does not include the error instance the method is called on - that is | ||
// looked at separately by the "errors" package in functions like "As". | ||
func (e *IBMProblem) Unwrap() []error { | ||
var errs []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. The changes to this method are the key component of this PR. These changes re-enable compatibility with methods like The changes to add |
||
|
||
// Include native (i.e. non-Problem) caused by errors in the | ||
// chain for compatibility with respect to downstream methods | ||
// like "errors.Is" or "errors.As". | ||
if e.nativeCausedBy != nil { | ||
errs = append(errs, e.nativeCausedBy) | ||
} | ||
|
||
causedBy := e.GetCausedBy() | ||
if causedBy == nil { | ||
return nil | ||
return errs | ||
} | ||
|
||
errs := []error{causedBy} | ||
errs = append(errs, causedBy) | ||
|
||
var toUnwrap interface{ Unwrap() []error } | ||
if errors.As(causedBy, &toUnwrap) { | ||
|
@@ -144,6 +160,8 @@ func ibmProblemf(err error, severity problemSeverity, component *ProblemComponen | |
var causedBy Problem | ||
if errors.As(err, &causedBy) { | ||
newError.causedBy = causedBy | ||
} else { | ||
newError.nativeCausedBy = err | ||
} | ||
|
||
return newError | ||
|
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.
@pyrooka Norbert - I want to publicly acknowledge that "you told me so" with respect to saving the previously returned errors in case we ever needed them! 😂
Turns out, we do need them - so this PR is adding everything back in just like you recommended in my last PR 😛
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.
Some developers never learn... 😂
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.
Haha, thanks for mentioning me, appreciate it! But I hope you didn't do this to protect yourself from future complications that might come up, so you can point at me and say "Hey, that was Norbert's idea, not mine..." 😂