pkg / errors

Simple error handling primitives
https://godoc.org/github.com/pkg/errors
BSD 2-Clause "Simplified" License
8.18k stars 691 forks source link

How to check if an error is wrapped or is original #210

Open xxjapp opened 5 years ago

xxjapp commented 5 years ago

Need method to check if error is wrapped

davecheney commented 5 years ago

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?

xxjapp commented 5 years ago

@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 commented 5 years ago

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

puellanivis commented 5 years ago

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
}
xxjapp commented 5 years ago

@puellanivis

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

puellanivis commented 5 years ago

Tech Debt… we meet again old friend…