errors icon indicating copy to clipboard operation
errors copied to clipboard

How to check if an error is wrapped or is original

Open xxjapp opened this issue 4 years ago • 6 comments

Need method to check if error is wrapped

xxjapp avatar Sep 05 '19 10:09 xxjapp

If you had that method, what would that enable you to do? Why is it important for your program to be able to detect if an error value wraps another?

davecheney avatar Sep 05 '19 10:09 davecheney

@davecheney

If I had the method, I could differentiate wrapped errors with call stack information and original errors without call stack information. For original errors I could add some call stack information by myself.

xxjapp avatar Sep 07 '19 11:09 xxjapp

For wrapped errors, I think there is no need to wrap it again when wrapping error from other's source.

xxjapp avatar Sep 07 '19 11:09 xxjapp

In general, either a source should guarantee that all of its errors are wrapped, or none of them are. If you absolutely must work with a broken design, then the following code does what you want it to do. Writing a helper function to do the same would save you no complexity:

if causer, ok := err.(interface{ Cause() error }); ok {
  // value is wrapped
}

puellanivis avatar Sep 07 '19 17:09 puellanivis

@puellanivis

Thank you for your detailed answer. For some reasons, I have to work with such a broken design from legacy source.

xxjapp avatar Sep 08 '19 01:09 xxjapp

Tech Debt… we meet again old friend…

puellanivis avatar Sep 10 '19 08:09 puellanivis