-
Notifications
You must be signed in to change notification settings - Fork 52
/
format.go
204 lines (181 loc) · 4.97 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package errors
import (
"bytes"
"encoding/json"
"fmt"
"strings"
)
// formatInfo contains all the error information.
type formatInfo struct {
code int
message string
err string
stack *stack
}
// Format implements fmt.Formatter. https://golang.org/pkg/fmt/#hdr-Printing
//
// Verbs:
// %s - Returns the user-safe error string mapped to the error code or
// ┊ the error message if none is specified.
// %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
//
// Examples:
// %s: error for internal read B
// %v: error for internal read B
// %-v: error for internal read B - #0 [/home/lk/workspace/golang/src/github.com/marmotedu/iam/main.go:12 (main.main)] (#100102) Internal Server Error
// %+v: error for internal read B - #0 [/home/lk/workspace/golang/src/github.com/marmotedu/iam/main.go:12 (main.main)] (#100102) Internal Server Error; error for internal read A - #1 [/home/lk/workspace/golang/src/github.com/marmotedu/iam/main.go:35 (main.newErrorB)] (#100104) Validation failed
// %#v: [{"error":"error for internal read B"}]
// %#-v: [{"caller":"#0 /home/lk/workspace/golang/src/github.com/marmotedu/iam/main.go:12 (main.main)","error":"error for internal read B","message":"(#100102) Internal Server Error"}]
// %#+v: [{"caller":"#0 /home/lk/workspace/golang/src/github.com/marmotedu/iam/main.go:12 (main.main)","error":"error for internal read B","message":"(#100102) Internal Server Error"},{"caller":"#1 /home/lk/workspace/golang/src/github.com/marmotedu/iam/main.go:35 (main.newErrorB)","error":"error for internal read A","message":"(#100104) Validation failed"}]
func (w *withCode) Format(state fmt.State, verb rune) {
switch verb {
case 'v':
str := bytes.NewBuffer([]byte{})
jsonData := []map[string]interface{}{}
var (
flagDetail bool
flagTrace bool
modeJSON bool
)
if state.Flag('#') {
modeJSON = true
}
if state.Flag('-') {
flagDetail = true
}
if state.Flag('+') {
flagTrace = true
}
sep := ""
errs := list(w)
length := len(errs)
for k, e := range errs {
finfo := buildFormatInfo(e)
jsonData, str = format(length-k-1, jsonData, str, finfo, sep, flagDetail, flagTrace, modeJSON)
sep = "; "
if !flagTrace {
break
}
if !flagDetail && !flagTrace && !modeJSON {
break
}
}
if modeJSON {
var byts []byte
byts, _ = json.Marshal(jsonData)
str.Write(byts)
}
fmt.Fprintf(state, "%s", strings.Trim(str.String(), "\r\n\t"))
default:
finfo := buildFormatInfo(w)
// Externally-safe error message
fmt.Fprintf(state, finfo.message)
}
}
func format(k int, jsonData []map[string]interface{}, str *bytes.Buffer, finfo *formatInfo,
sep string, flagDetail, flagTrace, modeJSON bool) ([]map[string]interface{}, *bytes.Buffer) {
if modeJSON {
data := map[string]interface{}{}
if flagDetail || flagTrace {
data = map[string]interface{}{
"message": finfo.message,
"code": finfo.code,
"error": finfo.err,
}
caller := fmt.Sprintf("#%d", k)
if finfo.stack != nil {
f := Frame((*finfo.stack)[0])
caller = fmt.Sprintf("%s %s:%d (%s)",
caller,
f.file(),
f.line(),
f.name(),
)
}
data["caller"] = caller
} else {
data["error"] = finfo.message
}
jsonData = append(jsonData, data)
} else {
if flagDetail || flagTrace {
if finfo.stack != nil {
f := Frame((*finfo.stack)[0])
fmt.Fprintf(str, "%s%s - #%d [%s:%d (%s)] (%d) %s",
sep,
finfo.err,
k,
f.file(),
f.line(),
f.name(),
finfo.code,
finfo.message,
)
} else {
fmt.Fprintf(str, "%s%s - #%d %s", sep, finfo.err, k, finfo.message)
}
} else {
fmt.Fprintf(str, finfo.message)
}
}
return jsonData, str
}
// list will convert the error stack into a simple array.
func list(e error) []error {
ret := []error{}
if e != nil {
if w, ok := e.(interface{ Unwrap() error }); ok {
ret = append(ret, e)
ret = append(ret, list(w.Unwrap())...)
} else {
ret = append(ret, e)
}
}
return ret
}
func buildFormatInfo(e error) *formatInfo {
var finfo *formatInfo
switch err := e.(type) {
case *fundamental:
finfo = &formatInfo{
code: unknownCoder.Code(),
message: err.msg,
err: err.msg,
stack: err.stack,
}
case *withStack:
finfo = &formatInfo{
code: unknownCoder.Code(),
message: err.Error(),
err: err.Error(),
stack: err.stack,
}
case *withCode:
coder, ok := codes[err.code]
if !ok {
coder = unknownCoder
}
extMsg := coder.String()
if extMsg == "" {
extMsg = err.err.Error()
}
finfo = &formatInfo{
code: coder.Code(),
message: extMsg,
err: err.err.Error(),
stack: err.stack,
}
default:
finfo = &formatInfo{
code: unknownCoder.Code(),
message: err.Error(),
err: err.Error(),
}
}
return finfo
}