-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathingest_error.go
More file actions
112 lines (97 loc) · 2.86 KB
/
ingest_error.go
File metadata and controls
112 lines (97 loc) · 2.86 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
package gin
import (
"fmt"
)
// IngestLayer identifies the ingest layer that rejected a document.
type IngestLayer string
const (
// IngestLayerParser identifies JSON parser failures for a document.
IngestLayerParser IngestLayer = "parser"
// IngestLayerTransformer identifies field transformer failures for a document.
IngestLayerTransformer IngestLayer = "transformer"
// IngestLayerNumeric identifies numeric coercion or promotion failures for a document.
IngestLayerNumeric IngestLayer = "numeric"
// IngestLayerSchema identifies unsupported value-shape failures for a document.
IngestLayerSchema IngestLayer = "schema"
)
// IngestError reports a hard per-document ingest failure.
//
// Path() returns the source JSONPath that rejected the document. Parser-level
// failures that are not attributable to a path report the empty string.
//
// Layer() identifies the ingest stage that rejected the document. Callers must
// tolerate future layer strings in addition to the built-in parser,
// transformer, numeric, and schema values.
//
// Value() returns a verbatim string representation of the offending input or
// value. The library does not redact or truncate it; callers that log
// untrusted documents own their redaction and output-size policy.
type IngestError struct {
path string
layer IngestLayer
value string
err error
}
// Path returns the source JSONPath that rejected the document.
func (e *IngestError) Path() string {
if e == nil {
return ""
}
return e.path
}
// Layer returns the ingest stage that rejected the document.
func (e *IngestError) Layer() IngestLayer {
if e == nil {
return ""
}
return e.layer
}
// Value returns the verbatim offending input or transformed value.
func (e *IngestError) Value() string {
if e == nil {
return ""
}
return e.value
}
// Error returns a stable human-readable message for the hard ingest failure.
// The "ingest <layer> failure [at <path>]: <cause>" format is API-stable.
func (e *IngestError) Error() string {
if e == nil {
return "<nil>"
}
if e.path == "" {
return fmt.Sprintf("ingest %s failure: %v", e.layer, e.err)
}
return fmt.Sprintf("ingest %s failure at %s: %v", e.layer, e.path, e.err)
}
// Unwrap returns the underlying cause for stdlib error unwrapping.
func (e *IngestError) Unwrap() error {
if e == nil {
return nil
}
return e.err
}
// Cause returns the underlying cause for github.com/pkg/errors compatibility.
func (e *IngestError) Cause() error {
if e == nil {
return nil
}
return e.err
}
func newIngestError(layer IngestLayer, path string, value any, err error) error {
if err == nil {
return nil
}
return newIngestErrorString(layer, path, fmt.Sprint(value), err)
}
func newIngestErrorString(layer IngestLayer, path string, value string, err error) error {
if err == nil {
return nil
}
return &IngestError{
path: path,
layer: layer,
value: value,
err: err,
}
}