-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherrata.go
635 lines (537 loc) · 15.1 KB
/
errata.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// Package errata is auto-generated by errata
// Errata Schema Version: 0.1
// Hash: 9f2e70162ab7eeef16b15ac74b8f1ad2
package errata
import (
"crypto/sha1"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
)
type erratum struct {
code string
message string
categories []string
args map[string]interface{}
labels map[string]string
guide string
file string
line int
uuid string
wrapped error
}
// TODO: add documentation to all public methods
// Erratum is the public interface which indicates that a given error is an Erratum.
type Erratum interface {
// behave like a regular error
error
Unwrap() error
Code() string
Message() string
Categories() []string
Args() map[string]interface{}
Guide() string
Labels() map[string]string
UUID() string
HelpURL() string
}
func (e *erratum) Unwrap() error {
return e.wrapped
}
func (e *erratum) UUID() string {
if e.uuid == "" {
e.uuid = generateReference(e.code)
}
return e.uuid
}
// Format controls the verbosity of the printed error.
func (e *erratum) Format(f fmt.State, verb rune) {
if verb == 'v' && f.Flag('+') {
args := func() string {
if len(e.Args()) <= 0 {
return ""
}
var args []string
for k, v := range e.Args() {
args = append(args, fmt.Sprintf(`%s="%+v"`, k, v))
}
return fmt.Sprintf(` (%s)`, strings.Join(args, ", "))
}()
f.Write([]byte(fmt.Sprintf("%s%s. For more details, see %s", e.Error(), args, e.HelpURL())))
if unwrapped := e.Unwrap(); unwrapped != nil {
f.Write([]byte("\n↳ "))
if e, ok := unwrapped.(fmt.Formatter); ok {
e.Format(f, verb)
} else if e, ok := unwrapped.(error); ok {
f.Write([]byte(e.Error()))
}
}
} else {
f.Write([]byte(e.Error()))
}
}
func (e *erratum) Error() string {
return fmt.Sprintf("[%s] <%s:%v> %s", e.code, e.file, e.line, e.message)
}
func (e *erratum) HelpURL() string {
return fmt.Sprintf("https://dannykopping.github.io/errata/errata/%s", e.code)
}
func (e *erratum) Code() string {
return e.code
}
func (e *erratum) Message() string {
return e.message
}
func (e *erratum) Categories() []string {
return e.categories
}
func (e *erratum) Args() map[string]interface{} {
return e.args
}
func (e *erratum) Labels() map[string]string {
return e.labels
}
func (e *erratum) Guide() string {
return e.guide
}
func (e *erratum) File() string {
return e.file
}
func (e *erratum) Line() int {
return e.line
}
const (
ArgumentLabelNameClashErrCode string = "errata-argument-label-name-clash"
CodeGenErrCode string = "errata-code-gen"
FileNotFoundErrCode string = "errata-file-not-found"
FileNotReadableErrCode string = "errata-file-not-readable"
InvalidDatasourceErrCode string = "errata-invalid-datasource"
InvalidDefinitionsErrCode string = "errata-invalid-definitions"
InvalidSyntaxErrCode string = "errata-invalid-syntax"
MarkdownRenderingErrCode string = "errata-markdown-rendering"
ServeMethodNotAllowedErrCode string = "errata-serve-method-not-allowed"
ServeSearchIndexErrCode string = "errata-serve-search-index"
ServeSearchMissingTermErrCode string = "errata-serve-search-missing-term"
ServeUnknownCodeErrCode string = "errata-serve-unknown-code"
ServeUnknownRouteErrCode string = "errata-serve-unknown-route"
ServeWebUiErrCode string = "errata-serve-web-ui"
TemplateExecutionErrCode string = "errata-template-execution"
)
type ArgumentLabelNameClashErr struct {
erratum
}
type CodeGenErr struct {
erratum
}
type FileNotFoundErr struct {
erratum
}
type FileNotReadableErr struct {
erratum
}
type InvalidDatasourceErr struct {
erratum
}
type InvalidDefinitionsErr struct {
erratum
}
type InvalidSyntaxErr struct {
erratum
}
type MarkdownRenderingErr struct {
erratum
}
type ServeMethodNotAllowedErr struct {
erratum
}
type ServeSearchIndexErr struct {
erratum
}
type ServeSearchMissingTermErr struct {
erratum
}
type ServeUnknownCodeErr struct {
erratum
}
type ServeUnknownRouteErr struct {
erratum
}
type ServeWebUiErr struct {
erratum
}
type TemplateExecutionErr struct {
erratum
}
func NewArgumentLabelNameClashErr(wrapped error, key string) *ArgumentLabelNameClashErr {
err := erratum{
code: ArgumentLabelNameClashErrCode,
message: `An error definition contains a label with the same name as an argument`,
categories: []string{"datasource", "validation"},
labels: map[string]string{
"severity": "fatal",
},
guide: `Error definitions must have labels with keys that are unique across the list of arguments`,
args: map[string]interface{}{
"key": key,
},
wrapped: wrapped,
}
addCaller(&err)
return &ArgumentLabelNameClashErr{err}
}
// GetKey returns the "key" argument for a ArgumentLabelNameClashErr instance.
func (e *ArgumentLabelNameClashErr) GetKey() interface{} {
return e.args["key"]
}
// GetSeverity returns the "severity" label for a ArgumentLabelNameClashErr instance.
func (e *ArgumentLabelNameClashErr) GetSeverity() string {
return "fatal"
}
func NewCodeGenErr(wrapped error) *CodeGenErr {
err := erratum{
code: CodeGenErrCode,
message: `Code generation failed`,
categories: []string{"codegen"},
labels: map[string]string{
"severity": "fatal",
},
guide: `The provided template may contain errors`,
args: map[string]interface{}{},
wrapped: wrapped,
}
addCaller(&err)
return &CodeGenErr{err}
}
// GetSeverity returns the "severity" label for a CodeGenErr instance.
func (e *CodeGenErr) GetSeverity() string {
return "fatal"
}
func NewFileNotFoundErr(wrapped error, path string) *FileNotFoundErr {
err := erratum{
code: FileNotFoundErrCode,
message: `File path is incorrect or inaccessible`,
categories: []string{"file"},
labels: map[string]string{
"severity": "fatal",
},
guide: `Ensure the given file exists and can be accessed by errata`,
args: map[string]interface{}{
"path": path,
},
wrapped: wrapped,
}
addCaller(&err)
return &FileNotFoundErr{err}
}
// GetPath returns the "path" argument for a FileNotFoundErr instance.
func (e *FileNotFoundErr) GetPath() interface{} {
return e.args["path"]
}
// GetSeverity returns the "severity" label for a FileNotFoundErr instance.
func (e *FileNotFoundErr) GetSeverity() string {
return "fatal"
}
func NewFileNotReadableErr(wrapped error, path string) *FileNotReadableErr {
err := erratum{
code: FileNotReadableErrCode,
message: `File is unreadable`,
categories: []string{"file"},
labels: map[string]string{
"severity": "fatal",
},
guide: `Ensure the given file has the correct permissions`,
args: map[string]interface{}{
"path": path,
},
wrapped: wrapped,
}
addCaller(&err)
return &FileNotReadableErr{err}
}
// GetPath returns the "path" argument for a FileNotReadableErr instance.
func (e *FileNotReadableErr) GetPath() interface{} {
return e.args["path"]
}
// GetSeverity returns the "severity" label for a FileNotReadableErr instance.
func (e *FileNotReadableErr) GetSeverity() string {
return "fatal"
}
func NewInvalidDatasourceErr(wrapped error, path string) *InvalidDatasourceErr {
err := erratum{
code: InvalidDatasourceErrCode,
message: `Datasource file is invalid`,
categories: []string{"datasource"},
labels: map[string]string{
"severity": "fatal",
},
guide: `Check the given datasource file for errors`,
args: map[string]interface{}{
"path": path,
},
wrapped: wrapped,
}
addCaller(&err)
return &InvalidDatasourceErr{err}
}
// GetPath returns the "path" argument for a InvalidDatasourceErr instance.
func (e *InvalidDatasourceErr) GetPath() interface{} {
return e.args["path"]
}
// GetSeverity returns the "severity" label for a InvalidDatasourceErr instance.
func (e *InvalidDatasourceErr) GetSeverity() string {
return "fatal"
}
func NewInvalidDefinitionsErr(wrapped error, path string) *InvalidDefinitionsErr {
err := erratum{
code: InvalidDefinitionsErrCode,
message: `One or more definitions declared in are invalid`,
categories: []string{"definitions", "validation"},
labels: map[string]string{
"severity": "fatal",
},
guide: ``,
args: map[string]interface{}{
"path": path,
},
wrapped: wrapped,
}
addCaller(&err)
return &InvalidDefinitionsErr{err}
}
// GetPath returns the "path" argument for a InvalidDefinitionsErr instance.
func (e *InvalidDefinitionsErr) GetPath() interface{} {
return e.args["path"]
}
// GetSeverity returns the "severity" label for a InvalidDefinitionsErr instance.
func (e *InvalidDefinitionsErr) GetSeverity() string {
return "fatal"
}
func NewInvalidSyntaxErr(wrapped error, path string) *InvalidSyntaxErr {
err := erratum{
code: InvalidSyntaxErrCode,
message: `File has syntax errors`,
categories: []string{"parsing"},
labels: map[string]string{
"severity": "fatal",
},
guide: ``,
args: map[string]interface{}{
"path": path,
},
wrapped: wrapped,
}
addCaller(&err)
return &InvalidSyntaxErr{err}
}
// GetPath returns the "path" argument for a InvalidSyntaxErr instance.
func (e *InvalidSyntaxErr) GetPath() interface{} {
return e.args["path"]
}
// GetSeverity returns the "severity" label for a InvalidSyntaxErr instance.
func (e *InvalidSyntaxErr) GetSeverity() string {
return "fatal"
}
func NewMarkdownRenderingErr(wrapped error) *MarkdownRenderingErr {
err := erratum{
code: MarkdownRenderingErrCode,
message: `Markdown rendering failed`,
categories: []string{"web-ui"},
labels: map[string]string{
"severity": "warning",
},
guide: ``,
args: map[string]interface{}{},
wrapped: wrapped,
}
addCaller(&err)
return &MarkdownRenderingErr{err}
}
// GetSeverity returns the "severity" label for a MarkdownRenderingErr instance.
func (e *MarkdownRenderingErr) GetSeverity() string {
return "warning"
}
func NewServeMethodNotAllowedErr(wrapped error, route string, method string) *ServeMethodNotAllowedErr {
err := erratum{
code: ServeMethodNotAllowedErrCode,
message: `Given HTTP method for requested route is not allowed`,
categories: []string{"serve", "web-ui"},
labels: map[string]string{
"severity": "warning",
},
guide: ``,
args: map[string]interface{}{
"route": route,
"method": method,
},
wrapped: wrapped,
}
addCaller(&err)
return &ServeMethodNotAllowedErr{err}
}
// GetRoute returns the "route" argument for a ServeMethodNotAllowedErr instance.
func (e *ServeMethodNotAllowedErr) GetRoute() interface{} {
return e.args["route"]
}
// GetMethod returns the "method" argument for a ServeMethodNotAllowedErr instance.
func (e *ServeMethodNotAllowedErr) GetMethod() interface{} {
return e.args["method"]
}
// GetSeverity returns the "severity" label for a ServeMethodNotAllowedErr instance.
func (e *ServeMethodNotAllowedErr) GetSeverity() string {
return "warning"
}
func NewServeSearchIndexErr(wrapped error) *ServeSearchIndexErr {
err := erratum{
code: ServeSearchIndexErrCode,
message: `Failed to build search index`,
categories: []string{"serve", "web-ui", "search"},
labels: map[string]string{
"severity": "fatal",
},
guide: ``,
args: map[string]interface{}{},
wrapped: wrapped,
}
addCaller(&err)
return &ServeSearchIndexErr{err}
}
// GetSeverity returns the "severity" label for a ServeSearchIndexErr instance.
func (e *ServeSearchIndexErr) GetSeverity() string {
return "fatal"
}
func NewServeSearchMissingTermErr(wrapped error) *ServeSearchMissingTermErr {
err := erratum{
code: ServeSearchMissingTermErrCode,
message: `Search request is missing a "term" query string parameter`,
categories: []string{"serve", "web-ui", "search"},
labels: map[string]string{
"severity": "warning",
},
guide: ``,
args: map[string]interface{}{},
wrapped: wrapped,
}
addCaller(&err)
return &ServeSearchMissingTermErr{err}
}
// GetSeverity returns the "severity" label for a ServeSearchMissingTermErr instance.
func (e *ServeSearchMissingTermErr) GetSeverity() string {
return "warning"
}
func NewServeUnknownCodeErr(wrapped error, code string) *ServeUnknownCodeErr {
err := erratum{
code: ServeUnknownCodeErrCode,
message: `Cannot find error definition for given code %q`,
categories: []string{"serve", "web-ui"},
labels: map[string]string{
"http_status_code": "404",
"severity": "warning",
},
guide: ``,
args: map[string]interface{}{
"code": code,
},
wrapped: wrapped,
}
addCaller(&err)
return &ServeUnknownCodeErr{err}
}
// GetCode returns the "code" argument for a ServeUnknownCodeErr instance.
func (e *ServeUnknownCodeErr) GetCode() interface{} {
return e.args["code"]
}
// GetHttpStatusCode returns the "http_status_code" label for a ServeUnknownCodeErr instance.
func (e *ServeUnknownCodeErr) GetHttpStatusCode() string {
return "404"
}
// GetSeverity returns the "severity" label for a ServeUnknownCodeErr instance.
func (e *ServeUnknownCodeErr) GetSeverity() string {
return "warning"
}
func NewServeUnknownRouteErr(wrapped error, route string) *ServeUnknownRouteErr {
err := erratum{
code: ServeUnknownRouteErrCode,
message: `Requested route not defined`,
categories: []string{"serve", "web-ui"},
labels: map[string]string{
"severity": "warning",
},
guide: ``,
args: map[string]interface{}{
"route": route,
},
wrapped: wrapped,
}
addCaller(&err)
return &ServeUnknownRouteErr{err}
}
// GetRoute returns the "route" argument for a ServeUnknownRouteErr instance.
func (e *ServeUnknownRouteErr) GetRoute() interface{} {
return e.args["route"]
}
// GetSeverity returns the "severity" label for a ServeUnknownRouteErr instance.
func (e *ServeUnknownRouteErr) GetSeverity() string {
return "warning"
}
func NewServeWebUiErr(wrapped error, path string) *ServeWebUiErr {
err := erratum{
code: ServeWebUiErrCode,
message: `Cannot serve web UI for datasource %q`,
categories: []string{"serve", "web-ui"},
labels: map[string]string{
"severity": "fatal",
},
guide: ``,
args: map[string]interface{}{
"path": path,
},
wrapped: wrapped,
}
addCaller(&err)
return &ServeWebUiErr{err}
}
// GetPath returns the "path" argument for a ServeWebUiErr instance.
func (e *ServeWebUiErr) GetPath() interface{} {
return e.args["path"]
}
// GetSeverity returns the "severity" label for a ServeWebUiErr instance.
func (e *ServeWebUiErr) GetSeverity() string {
return "fatal"
}
func NewTemplateExecutionErr(wrapped error) *TemplateExecutionErr {
err := erratum{
code: TemplateExecutionErrCode,
message: `Error in template execution`,
categories: []string{"codegen"},
labels: map[string]string{
"severity": "fatal",
},
guide: ``,
args: map[string]interface{}{},
wrapped: wrapped,
}
addCaller(&err)
return &TemplateExecutionErr{err}
}
// GetSeverity returns the "severity" label for a TemplateExecutionErr instance.
func (e *TemplateExecutionErr) GetSeverity() string {
return "fatal"
}
func addCaller(err *erratum) {
_, file, line, ok := runtime.Caller(3)
if ok {
paths := strings.Split(file, string(os.PathSeparator))
segments := 2
if len(paths) < segments {
segments = 1
}
err.file = filepath.Join(paths[len(paths)-segments:]...)
err.line = line
}
}
func generateReference(code string) string {
return fmt.Sprintf("%x", sha1.Sum([]byte(code+time.Now().Format(time.RFC3339Nano))))
}