1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
// Package xerrors contains extensions to the standard library package errors.
package xerrors
import (
"errors"
"runtime"
"strconv"
"strings"
)
type withStack struct {
inner error
pc []uintptr
}
func (err withStack) Error() string {
var sb strings.Builder
frames := runtime.CallersFrames(err.pc)
_, _ = sb.WriteString(err.inner.Error())
_, _ = sb.WriteString("\n\n")
for {
frame, more := frames.Next()
_, _ = sb.WriteString(frame.Function)
_, _ = sb.WriteString("(...)\n ")
_, _ = sb.WriteString(frame.File)
_, _ = sb.WriteString(":")
_, _ = sb.WriteString(strconv.Itoa(frame.Line))
_, _ = sb.WriteString("\n")
if !more {
break
}
}
return sb.String()
}
func (err withStack) Unwrap() error {
return err.inner
}
var noError = errors.New("no error")
// WithStack returns an error that wraps err and adds the call stack of the call to WithStack to
// Error(). If err is nil or already has a stack attached, returns err.
func WithStack(err error) error {
if err == nil {
return nil
}
// use noError in case anything along err's chain has a custom Is that calls Error() for some
// reason.
if errors.Is(err, withStack{inner: noError}) {
return err
}
var buf [64]uintptr
var ptrs []uintptr
skip := 2
for {
n := runtime.Callers(skip, buf[:])
ptrs = append(ptrs, buf[:n]...)
if n < len(buf) {
break
}
skip += n
}
return withStack{
inner: err,
pc: ptrs,
}
}
|