-
Notifications
You must be signed in to change notification settings - Fork 4
/
format.go
156 lines (140 loc) · 3.82 KB
/
format.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package errors
import (
"bytes"
"encoding/json"
"fmt"
"path"
"runtime"
"strings"
)
// Format implements fmt.Formatter. https://golang.org/pkg/fmt/#hdr-Printing
//
// Verbs:
// %s Returns the error string of the last error added
// %v Alias for %s
//
// Flags:
// # JSON formatted output, useful for logging
// - Output caller details, useful for troubleshooting
// Output full error stack details, useful for debugging
// ' ' (space) Add whitespace formatting for readability, useful for development
//
// Examples:
// %s: An error occurred
// %v: An error occurred
// %-v: #0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors) - An error occurred
// % v: #0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors) - An error occurred #1 stack_test.go:39 (github.com/bdlm/error_test.TestErrors) - An error occurred
// %#v: {"error":"An error occurred"}
// %#-v: {"caller":"#0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors)","error":"An error occurred"}
// %# v: [{"caller":"#0 stack_test.go:40 (github.com/bdlm/error_test.TestErrors)","error":"An error occurred"},{"caller":"#0 stack_test.go:39 (github.com/bdlm/error_test.TestErrors)","error":"An error occurred"}]
func (e *E) Format(state fmt.State, verb rune) {
str := bytes.NewBuffer([]byte{})
switch verb {
default:
fmt.Fprint(str, e.Error())
case 'v':
var (
flagDetail bool
flagFormat bool
flagTrace bool
modeJSON bool
lastE error
key int
nextE error
)
if state.Flag('#') {
modeJSON = true
}
if state.Flag(' ') {
flagFormat = true
}
if state.Flag('-') {
flagDetail = true
}
if state.Flag(' ') {
flagTrace = true
}
jsonData := []map[string]interface{}{}
sp := ""
for key, nextE = range list(e) {
sp, jsonData, str = format(key, nextE, sp, jsonData, str, flagDetail, flagFormat, flagTrace, modeJSON)
if !flagTrace {
break
}
if !flagDetail &&
!flagFormat &&
!flagTrace &&
!modeJSON {
break
}
if err, ok := nextE.(*E); ok {
lastE = err.prev
}
}
if nil != lastE {
_, jsonData, str = format(key 1, lastE, sp, jsonData, str, flagDetail, flagFormat, flagTrace, modeJSON)
}
if modeJSON {
var byts []byte
if flagFormat {
byts, _ = json.MarshalIndent(jsonData, "", " ")
} else {
byts, _ = json.Marshal(jsonData)
}
str.Write(byts)
}
}
fmt.Fprintf(state, "%s", strings.Trim(str.String(), "\r\n\t"))
}
func format(key int, nextE error, sp string, jsonData []map[string]interface{}, str *bytes.Buffer, flagDetail bool, flagFormat bool, flagTrace bool, modeJSON bool) (string, []map[string]interface{}, *bytes.Buffer) {
err, ok := nextE.(*E)
if modeJSON {
data := map[string]interface{}{}
if flagDetail || flagTrace {
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,
)
}
}
if "" != nextE.Error() {
data["error"] = nextE.Error()
}
jsonData = append(jsonData, data)
} else {
if "" != nextE.Error() {
fmt.Fprintf(str, "%s%s", sp, nextE.Error())
}
if flagDetail || flagTrace {
if "" != nextE.Error() {
fmt.Fprintf(str, " - ")
}
if ok && nil != err.Caller() {
fmt.Fprintf(str, "#%d %s:%d (%s);",
key,
path.Base(err.Caller().File()),
err.Caller().Line(),
runtime.FuncForPC(err.Caller().Pc()).Name(),
)
} else {
fmt.Fprintf(str, "#%d n/a",
key,
)
}
}
if flagFormat {
str = bytes.NewBuffer([]byte(strings.Trim(str.String(), " ")))
fmt.Fprintf(str, "\n")
} else if flagTrace {
sp = " "
}
}
return sp, jsonData, str
}