...
1 package pkgerrors
2
3 import (
4 "github.com/pkg/errors"
5 )
6
7 var (
8 StackSourceFileName = "source"
9 StackSourceLineName = "line"
10 StackSourceFunctionName = "func"
11 )
12
13 type state struct {
14 b []byte
15 }
16
17
18 func (s *state) Write(b []byte) (n int, err error) {
19 s.b = b
20 return len(b), nil
21 }
22
23
24 func (s *state) Width() (wid int, ok bool) {
25 return 0, false
26 }
27
28
29 func (s *state) Precision() (prec int, ok bool) {
30 return 0, false
31 }
32
33
34 func (s *state) Flag(c int) bool {
35 return false
36 }
37
38 func frameField(f errors.Frame, s *state, c rune) string {
39 f.Format(s, c)
40 return string(s.b)
41 }
42
43
44
45
46 func MarshalStack(err error) interface{} {
47 type stackTracer interface {
48 StackTrace() errors.StackTrace
49 }
50 sterr, ok := err.(stackTracer)
51 if !ok {
52 return nil
53 }
54 st := sterr.StackTrace()
55 s := &state{}
56 out := make([]map[string]string, 0, len(st))
57 for _, frame := range st {
58 out = append(out, map[string]string{
59 StackSourceFileName: frameField(frame, s, 's'),
60 StackSourceLineName: frameField(frame, s, 'd'),
61 StackSourceFunctionName: frameField(frame, s, 'n'),
62 })
63 }
64 return out
65 }
66
View as plain text