zlog/pkgerrors/stacktrace.go

38 lines
811 B
Go
Raw Normal View History

package pkgerrors
import (
"fmt"
"github.com/pkg/errors"
)
var (
StackSourceFileName = "source"
StackSourceLineName = "line"
StackSourceFunctionName = "func"
)
// MarshalStack implements pkg/errors stack trace marshaling.
//
// zerolog.ErrorStackMarshaler = MarshalStack
2019-01-02 19:12:56 +00:00
func MarshalStack(err error) interface{} {
type stackTracer interface {
StackTrace() errors.StackTrace
}
var st errors.StackTrace
if err, ok := err.(stackTracer); ok {
st = err.StackTrace()
} else {
return nil
}
2019-01-02 19:12:56 +00:00
out := make([]map[string]string, 0, len(st))
for _, frame := range st {
out = append(out, map[string]string{
StackSourceFileName: fmt.Sprintf("%s", frame),
StackSourceLineName: fmt.Sprintf("%d", frame),
StackSourceFunctionName: fmt.Sprintf("%n", frame),
})
}
2019-01-02 19:12:56 +00:00
return out
}