4 ms·
A deferred function is called at the line where return would happen, so you can still access stack trace information required for errors.Trace(err) from inside
by akavel 7y ago
A deferred function is called at the line where return would happen, so you can still access stack trace information required for errors.Trace(err) from inside a deferred function. You just need to go "one frame higher". (See the stack trace printed in: https://play.golang.org/p/Bpqdm8oWBF3 https://play.golang.org/p/Bpqdm8oWBF3).
As a result, I believe juju/errors could then be extended with a new function, to be used like this:
func foobar(...) (..., err error) {
defer errors.Tracify(&err)
...
}
where:
package juju/errors
func Tracify(err *error) {
if *err != nil {
*err = TraceFromDefer(*err)
}
}