-
Notifications
You must be signed in to change notification settings - Fork 699
feat ExistStack: Add a public method to determine if the error has been appended to the stack. #239
Changes from 2 commits
8aa1409
b3e14fa
5969e20
88c8636
0247cb8
5c191f9
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 |
---|---|---|
|
@@ -106,6 +106,23 @@ func New(message string) error { | |
} | ||
} | ||
|
||
// ExistStack return an error already with stack | ||
// ExistStack will check all parent error | ||
func ExistStack(err error) bool { | ||
type stackTracer interface { | ||
StackTrace() StackTrace | ||
} | ||
if _, ok := err.(stackTracer); ok { | ||
return true | ||
} | ||
type causer interface { | ||
Cause() error | ||
} | ||
if _, ok := err.(causer); !ok { | ||
return false | ||
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. Every code path in the preceding Considering using:
Please check the implementation of 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. Thank you very much for your help |
||
} | ||
return ExistStack(err.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. Here, you attempt to call the receiver method 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. 🤔 Not sure if the Go compiler is smart enough to rework end-recursion into the more efficient for-loop. |
||
} | ||
// Errorf formats according to a format specifier and returns the string | ||
// as a value that satisfies error. | ||
// Errorf also records the stack trace at the point it was called. | ||
|
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.
Just checking
Cause() error
ignores the possibility of anUnwrap() error
which is kind of the preferred way of unwrapping errors now. So, this would only work with errors from this package, and those designed specifically for pre-unwrap compatibility with this package, which is no longer a strong argument since go1.13.