-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconf.go
More file actions
400 lines (339 loc) · 10.5 KB
/
conf.go
File metadata and controls
400 lines (339 loc) · 10.5 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package errific
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"
)
// Configure errific options.
func Configure(opts ...Option) {
cMu.Lock()
defer cMu.Unlock()
// defaults
c.caller = Suffix
c.layout = Newline
c.withStack = false
c.trimPrefixes = nil
c.trimCWD = false
c.outputFormat = OutputJSON
c.verbosity = VerbosityFull
// Default field visibility (used when verbosity is VerbosityFull or VerbosityCustom)
c.showCode = true
c.showCategory = true
c.showContext = true
c.showHTTPStatus = true
c.showRetryMetadata = true
c.showMCPData = true
c.showTags = true
c.showLabels = true
c.showTimestamps = true
for _, opt := range opts {
switch o := opt.(type) {
case callerOption:
c.caller = o
case layoutOption:
c.layout = o
case withStackTraceOption:
c.withStack = o
case trimPrefixesOption:
c.trimPrefixes = o.Prefixes()
case trimCWDOption:
c.trimCWD = o
case outputFormatOption:
c.outputFormat = o
case verbosityOption:
c.verbosity = o
// Set field visibility based on verbosity level
switch o {
case VerbosityMinimal:
c.showCode = false
c.showCategory = false
c.showContext = false
c.showHTTPStatus = false
c.showRetryMetadata = false
c.showMCPData = false
c.showTags = false
c.showLabels = false
c.showTimestamps = false
case VerbosityStandard:
c.showCode = true
c.showCategory = true
c.showContext = true
c.showHTTPStatus = false
c.showRetryMetadata = false
c.showMCPData = false
c.showTags = false
c.showLabels = false
c.showTimestamps = false
case VerbosityFull:
c.showCode = true
c.showCategory = true
c.showContext = true
c.showHTTPStatus = true
c.showRetryMetadata = true
c.showMCPData = true
c.showTags = true
c.showLabels = true
c.showTimestamps = true
}
case fieldVisibilityOption:
// When using field visibility options, automatically switch to VerbosityCustom
if c.verbosity != VerbosityCustom {
c.verbosity = VerbosityCustom
}
// Apply the specific field visibility setting
switch o.field {
case "code":
c.showCode = o.show
case "category":
c.showCategory = o.show
case "context":
c.showContext = o.show
case "http_status":
c.showHTTPStatus = o.show
case "retry_metadata":
c.showRetryMetadata = o.show
case "mcp_data":
c.showMCPData = o.show
case "tags":
c.showTags = o.show
case "labels":
c.showLabels = o.show
case "timestamps":
c.showTimestamps = o.show
}
}
}
if c.trimCWD {
cwd, err := os.Getwd()
if err != nil {
// Fallback to not trimming CWD if we can't get it
c.trimCWD = false
return
}
// Trim the current working directory itself, not its parent
c.trimPrefixes = append([]string{cwd + "/"}, c.trimPrefixes...)
}
}
var (
c struct {
// Caller will configure the caller: Suffix|Prefix|Disabled.
// Default is Suffix.
caller callerOption
// Layout will configure the layout of wrapped errors: Newline|Inline.
// Default is Newline.
layout layoutOption
// WithStack will append stacktrace to end of message.
// Default is not including the stack.
withStack withStackTraceOption
// TrimPrefixes will trim prefixes from caller frame filenames.
trimPrefixes []string
// TrimCWD will trim the current working directory from filenames.
// Default is false.
trimCWD trimCWDOption
// Output format: Pretty, JSON, or Compact.
// Default is Pretty.
outputFormat outputFormatOption
// Verbosity controls which fields are shown in Error() output.
// Default is VerbosityFull (show all non-empty fields).
verbosity verbosityOption
// Field visibility flags (used when verbosity is VerbosityCustom)
showCode bool
showCategory bool
showContext bool
showHTTPStatus bool
showRetryMetadata bool
showMCPData bool
showTags bool
showLabels bool
showTimestamps bool
}
cMu sync.RWMutex
)
type callerOption int
func (callerOption) ErrificOption() {}
const (
// Suffix adds caller information at the end of the error message.
// This is default.
Suffix callerOption = iota
// Prefix adds caller information at the beginning of the error message.
Prefix
// Disabled does not include caller information in the error message.
Disabled
)
type layoutOption int
func (layoutOption) ErrificOption() {}
const (
// Newline joins errors with \n.
// This is default.
Newline layoutOption = iota
// Inline wraps errors with ↩.
Inline
)
type withStackTraceOption bool
func (withStackTraceOption) ErrificOption() {}
const (
// Include stacktrace in error message.
WithStack withStackTraceOption = true
)
type trimPrefixesOption struct {
prefixes []string
}
func (trimPrefixesOption) ErrificOption() {}
func (t trimPrefixesOption) Prefixes() []string {
return t.prefixes
}
var (
// TrimPrefixes from caller frame filenames.
TrimPrefixes = func(prefixes ...string) trimPrefixesOption {
return trimPrefixesOption{prefixes: prefixes}
}
)
type trimCWDOption bool
func (trimCWDOption) ErrificOption() {}
const (
// Trim current working directory from filenames.
TrimCWD trimCWDOption = true
)
type Option interface {
ErrificOption()
}
// outputFormatOption controls the format of error string output.
type outputFormatOption int
func (outputFormatOption) ErrificOption() {}
const (
// OutputPretty formats errors as human-readable multi-line text with all metadata.
//
// Example:
// user not found [main.go:20.GetUser]
// code: USER_404
// context: {user_id: user-123, source: database}
// http_status: 400
OutputPretty outputFormatOption = iota
// OutputJSON formats errors as compact JSON.
// This is the default.
// Useful for structured logging and machine processing.
//
// Example:
// {"error":"user not found","caller":"main.go:20","code":"USER_404",...}
OutputJSON
// OutputJSONPretty formats errors as indented JSON.
// Useful for documentation, debugging, and human-readable JSON output.
//
// Example:
// {
// "error": "user not found",
// "code": "USER_404",
// "caller": "main.go:20"
// }
OutputJSONPretty
// OutputCompact formats errors as single-line text with key=value pairs.
// Useful for log aggregation systems.
//
// Example:
// user not found [main.go:20] code=USER_404 user_id=user-123 http_status=400
OutputCompact
)
// verbosityOption controls which fields are included in Error() output.
type verbosityOption int
func (verbosityOption) ErrificOption() {}
const (
// VerbosityMinimal shows only the error message and caller.
//
// Example:
// user not found [main.go:20.GetUser]
VerbosityMinimal verbosityOption = iota
// VerbosityStandard shows message, caller, code, category, and context.
// Good balance for most applications.
//
// Example:
// user not found [main.go:20.GetUser]
// code: USER_404
// category: validation
// context: {user_id: user-123}
VerbosityStandard
// VerbosityFull shows all non-empty fields (default).
// Recommended for debugging and development.
//
// Example:
// user not found [main.go:20.GetUser]
// code: USER_404
// category: validation
// context: {user_id: user-123, source: database}
// http_status: 400
// retryable: true
// correlation_id: trace-123
// help: Check if user exists
VerbosityFull
// VerbosityCustom allows fine-grained control via individual field flags.
// Use with Show* and Hide* options.
VerbosityCustom
)
// Field visibility options for VerbosityCustom.
type fieldVisibilityOption struct {
field string
show bool
}
func (fieldVisibilityOption) ErrificOption() {}
var (
// ShowCode includes error code in output.
ShowCode = fieldVisibilityOption{field: "code", show: true}
// HideCode excludes error code from output.
HideCode = fieldVisibilityOption{field: "code", show: false}
// ShowCategory includes error category in output.
ShowCategory = fieldVisibilityOption{field: "category", show: true}
// HideCategory excludes error category from output.
HideCategory = fieldVisibilityOption{field: "category", show: false}
// ShowContext includes structured context in output.
ShowContext = fieldVisibilityOption{field: "context", show: true}
// HideContext excludes structured context from output.
HideContext = fieldVisibilityOption{field: "context", show: false}
// ShowHTTPStatus includes HTTP status code in output.
ShowHTTPStatus = fieldVisibilityOption{field: "http_status", show: true}
// HideHTTPStatus excludes HTTP status code from output.
HideHTTPStatus = fieldVisibilityOption{field: "http_status", show: false}
// ShowRetryMetadata includes retry information (retryable, retry_after, max_retries) in output.
ShowRetryMetadata = fieldVisibilityOption{field: "retry_metadata", show: true}
// HideRetryMetadata excludes retry information from output.
HideRetryMetadata = fieldVisibilityOption{field: "retry_metadata", show: false}
// ShowMCPData includes MCP-related fields (correlation_id, help, suggestion, etc.) in output.
ShowMCPData = fieldVisibilityOption{field: "mcp_data", show: true}
// HideMCPData excludes MCP-related fields from output.
HideMCPData = fieldVisibilityOption{field: "mcp_data", show: false}
// ShowTags includes semantic tags in output.
ShowTags = fieldVisibilityOption{field: "tags", show: true}
// HideTags excludes semantic tags from output.
HideTags = fieldVisibilityOption{field: "tags", show: false}
// ShowLabels includes key-value labels in output.
ShowLabels = fieldVisibilityOption{field: "labels", show: true}
// HideLabels excludes key-value labels from output.
HideLabels = fieldVisibilityOption{field: "labels", show: false}
// ShowTimestamps includes timestamp and duration in output.
ShowTimestamps = fieldVisibilityOption{field: "timestamps", show: true}
// HideTimestamps excludes timestamp and duration from output.
HideTimestamps = fieldVisibilityOption{field: "timestamps", show: false}
)
var root string
var goroot string
func init() {
_, file, _, _ := runtime.Caller(0)
root = fmt.Sprintf("%s/", filepath.Join(filepath.Dir(file), ".."))
// Try to get GOROOT using "go env GOROOT" first (preferred method)
if cmd := exec.Command("go", "env", "GOROOT"); cmd != nil {
if output, err := cmd.Output(); err == nil {
trimmed := strings.TrimSpace(string(output))
if trimmed != "" {
goroot = trimmed
return
}
}
}
// Fallback to runtime.GOROOT() if command failed
// Note: runtime.GOROOT() is deprecated but still works as a fallback
if fallback := runtime.GOROOT(); fallback != "" {
goroot = fallback
}
}