5 ms·
A nifty code snippet I use for this: // ErrLine returns an error formatted to include the line // number of where it occurred (presumably with a performan
by ben_jones 8y ago
A nifty code snippet I use for this:
// ErrLine returns an error formatted to include the line
// number of where it occurred (presumably with a performance penalty)
func ErrLine(err error) error {
if err != nil {
// notice that we're using 1, so it will actually log the where
// the error happened, 0 = this function, we don't want that.
pc, fn, line, _ := runtime.Caller(1)
err = fmt.Errorf("[error] in %s[%s:%d]: %s",
runtime.FuncForPC(pc).Name(), fn, line, err.Error())
}
return err
}