-
Notifications
You must be signed in to change notification settings - Fork 4
/
marshal.go
60 lines (55 loc) · 1.21 KB
/
marshal.go
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
package errors
import (
"encoding/json"
"fmt"
"path"
"runtime"
)
// MarshalJSON implements the json.Marshaller interface.
func (e *E) MarshalJSON() ([]byte, error) {
var lastE, nextE error
var key int
jsonData := []map[string]interface{}{}
for key, nextE = range list(e) {
data := map[string]interface{}{}
err, ok := nextE.(*E)
if ok && nil != err.Caller() {
data["caller"] = fmt.Sprintf("#%d %s:%d (%s)",
key,
path.Base(err.Caller().File()),
err.Caller().Line(),
runtime.FuncForPC(err.Caller().Pc()).Name(),
)
} else {
data["caller"] = fmt.Sprintf("#%d n/a",
key,
)
}
lastE = err.prev
if "" != nextE.Error() {
data["error"] = err.Error()
}
jsonData = append(jsonData, data)
}
if nil != lastE {
data := map[string]interface{}{}
err, ok := lastE.(*E)
if ok && nil != err.Caller() {
data["caller"] = fmt.Sprintf("#%d %s:%d (%s)",
key+1,
path.Base(err.Caller().File()),
err.Caller().Line(),
runtime.FuncForPC(err.Caller().Pc()).Name(),
)
} else {
data["caller"] = fmt.Sprintf("#%d n/a",
key+1,
)
}
if "" != lastE.Error() {
data["error"] = lastE.Error()
}
jsonData = append(jsonData, data)
}
return json.Marshal(jsonData)
}