-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
179 lines (163 loc) · 6.14 KB
/
errors.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
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
package json
import (
"fmt"
gdterrors "github.com/gdt-dev/gdt/errors"
"gopkg.in/yaml.v3"
)
var (
// ErrJSONPathInvalid returns an ErrParse when a JSONPath expression could
// not be parsed.
ErrJSONPathInvalid = fmt.Errorf(
"%w: JSONPath invalid", gdterrors.ErrParse,
)
// ErrJSONPathInvalidNoRoot returns an ErrParse when a JSONPath expression
// does not start with '$'
ErrJSONPathInvalidNoRoot = fmt.Errorf(
"%w: expression must start with '$'", ErrJSONPathInvalid,
)
// ErrJSONUnmarshalError is returned when JSON content cannot be decoded
ErrJSONUnmarshalError = fmt.Errorf(
"%w: failed to unmarshal JSON", gdterrors.ErrFailure,
)
// ErrJSONPathNotFound returns an ErrFailure when a JSONPath expression
// could not evaluate to a found element.
ErrJSONPathNotFound = fmt.Errorf(
"%w: failed to find element at JSONPath", gdterrors.ErrFailure,
)
// ErrJSONPathConversionError returns an ErrFailure when a JSONPath
// expression evaluated to a found element but could not be converted to a
// string.
ErrJSONPathConversionError = fmt.Errorf(
"%w: JSONPath value could not be compared", gdterrors.ErrFailure,
)
// ErrJSONPathNotEqual returns an ErrFailure when a JSONPath
// expression evaluated to a found element but the value did not match an
// expected string.
ErrJSONPathNotEqual = fmt.Errorf(
"%w: JSONPath values not equal", gdterrors.ErrFailure,
)
// ErrJSONSchemaValidateError returns an ErrFailure when a JSONSchema could
// not be parsed.
ErrJSONSchemaValidateError = fmt.Errorf(
"%w: failed to parse JSONSchema", gdterrors.ErrFailure,
)
// ErrJSONSchemaInvalid returns an ErrFailure when some content could not
// be validated with a JSONSchema.
ErrJSONSchemaInvalid = fmt.Errorf(
"%w: JSON content did not adhere to JSONSchema", gdterrors.ErrFailure,
)
// ErrJSONFormatError returns an ErrFailure when a JSONFormat expression
// could not evaluate to a found element.
ErrJSONFormatError = fmt.Errorf(
"%w: failed to determine JSON format", gdterrors.ErrFailure,
)
// ErrJSONFormatNotEqual returns an ErrFailure when a an element at a
// JSONPath was not in the expected format.
ErrJSONFormatNotEqual = fmt.Errorf(
"%w: JSON format not equal", gdterrors.ErrFailure,
)
// ErrJSONSchemaFileNotFound indicates a specified JSONSchema file could
// not be found.
ErrJSONSchemaFileNotFound = fmt.Errorf(
"%w: unable to find JSONSchema file",
gdterrors.ErrParse,
)
// ErrUnsupportedJSONSchemaReference indicates that a specified JSONSchema
// file is referenced as an HTTP(S) URL instead of a file URI.
ErrUnsupportedJSONSchemaReference = fmt.Errorf(
"%w: unsupported JSONSchema reference",
gdterrors.ErrParse,
)
)
// UnsupportedJSONSchemaReference returns ErrUnsupportedJSONSchemaReference for
// a supplied URL.
func UnsupportedJSONSchemaReference(url string, node *yaml.Node) error {
return fmt.Errorf(
"%w: %s at line %d, column %d",
ErrUnsupportedJSONSchemaReference, url, node.Line, node.Column,
)
}
// JSONSchemaFileNotFound returns ErrJSONSchemaFileNotFound for a supplied
// path.
func JSONSchemaFileNotFound(path string, node *yaml.Node) error {
return fmt.Errorf(
"%w: %s at line %d, column %d",
ErrJSONSchemaFileNotFound, path, node.Line, node.Column,
)
}
// JSONUnmarshalError returns an ErrFailure when JSON content cannot be
// decoded.
func JSONUnmarshalError(err error, node *yaml.Node) error {
if node != nil {
return fmt.Errorf(
"%w: %s at line %d, column %d",
ErrJSONUnmarshalError, err, node.Line, node.Column,
)
} else {
return fmt.Errorf("%w: %s", ErrJSONUnmarshalError, err)
}
}
// JSONPathInvalid returns an ErrParse when a JSONPath expression could not be
// parsed.
func JSONPathInvalid(path string, err error, node *yaml.Node) error {
return fmt.Errorf(
"%w: %s: %s at line %d, column %d",
ErrJSONPathInvalid, path, err, node.Line, node.Column,
)
}
// JSONPathInvalidNoRoot returns an ErrJSONPathInvalidNoRoot when a JSONPath
// expression does not start with '$'.
func JSONPathInvalidNoRoot(path string, node *yaml.Node) error {
return fmt.Errorf(
"%w: %s at line %d, column %d",
ErrJSONPathInvalidNoRoot, path, node.Line, node.Column,
)
}
// JSONPathNotFound returns an ErrFailure when a JSONPath expression could not
// evaluate to a found element.
func JSONPathNotFound(path string, err error) error {
return fmt.Errorf("%w: %s: %s", ErrJSONPathNotFound, path, err)
}
// JSONPathConversionError returns an ErrFailure when a JSONPath expression
// evaluated to a found element but the expected and found value types were
// incomparable.
func JSONPathConversionError(path string, exp interface{}, got interface{}) error {
return fmt.Errorf(
"%w: expected value of %v could not be compared to value %v at %s",
ErrJSONPathConversionError, exp, got, path,
)
}
// JSONPathValueNotEqual returns an ErrFailure when a JSONPath expression
// evaluated to a found element but the value did not match an expected string.
func JSONPathNotEqual(path string, exp interface{}, got interface{}) error {
return fmt.Errorf(
"%w: expected %v but got %v at %s",
ErrJSONPathNotEqual, exp, got, path,
)
}
// JSONSchemaValidateError returns an ErrFailure when a JSONSchema could not be
// parsed.
func JSONSchemaValidateError(path string, err error) error {
return fmt.Errorf("%w %s: %s", ErrJSONSchemaValidateError, path, err)
}
// JSONSchemaInvalid returns an ErrFailure when some content could not be
// validated with a JSONSchema.
func JSONSchemaInvalid(path string, err error) error {
return fmt.Errorf("%w %s: %s", ErrJSONSchemaInvalid, path, err)
}
// JSONFormatError returns an ErrFailure when a JSONFormat expression could not
// evaluate to a found element.
func JSONFormatError(format string, err error) error {
return fmt.Errorf("%w %s: %s", ErrJSONFormatError, format, err)
}
// JSONFormatNotEqual returns an ErrFailure when a an element at a JSONPath was
// not in the expected format.
func JSONFormatNotEqual(path string, exp string) error {
return fmt.Errorf(
"%w: element at %s was not in expected JSON format %s",
ErrJSONFormatNotEqual, path, exp,
)
}