-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
201 lines (168 loc) · 4.47 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package openapi
import (
"errors"
"fmt"
"strings"
"github.com/Masterminds/semver"
"github.com/chanced/jsonpointer"
"github.com/chanced/uri"
)
var (
ErrEmptyRef = errors.New("openapi: empty $ref")
ErrNotFound = fmt.Errorf("openapi: component not found")
ErrNotResolvable = errors.New("openapi: pointer path not resolvable")
ErrMissingOpenAPIVersion = errors.New("openapi: missing openapi version")
// ErrInvalidSemVer is returned a version is found to be invalid when
// being parsed.
ErrInvalidSemVer = errors.New("invalid semantic version")
// ErrInvalidMetadata is returned when the metadata of a semver is an invalid format
ErrInvalidSemVerMetadata = errors.New("invalid semantic version metadata string")
// ErrInvalidPrerelease is returned when the pre-release of a semver is an invalid format
ErrInvalidSemVerPrerelease = errors.New("invalid semantic version prerelease string")
ErrInvalidResolution = errors.New("openapi: invalid resolution")
)
type Error struct {
Err error
ResourceURI uri.URI
}
func NewError(err error, resource uri.URI) error {
return &Error{
Err: err,
ResourceURI: resource,
}
}
func (e *Error) Error() string {
return fmt.Sprintf("%s: [%q]", e.Err, e.ResourceURI.String())
}
func (e *Error) Unwrap() error {
return e.Err
}
func newErrNotFound(uri uri.URI, tok jsonpointer.Token) error {
return NewError(fmt.Errorf("%w: %q", ErrNotFound, tok), uri)
}
func newErrNotResolvable(uri uri.URI, tok jsonpointer.Token) error {
return NewError(fmt.Errorf("%w: %q", ErrNotResolvable, tok), uri)
}
type UnsupportedVersionError struct {
Version string `json:"version"`
Errs []error `json:"errors"`
}
func (e *UnsupportedVersionError) Error() string {
b := strings.Builder{}
b.WriteString("openapi: unsupported version:")
b.WriteString(fmt.Sprintf(" %q:", e.Version))
for _, err := range e.Errs {
b.WriteString(fmt.Sprintf("\n- %s", err))
}
return b.String()
}
func (e *UnsupportedVersionError) As(target interface{}) bool {
for _, v := range e.Errs {
if errors.As(v, target) {
return true
}
}
return false
}
func (e *UnsupportedVersionError) Is(err error) bool {
for _, v := range e.Errs {
if errors.Is(v, err) {
return true
}
}
return false
}
type ValidationError struct {
Kind Kind
Err error
URI uri.URI
}
func NewValidationError(err error, kind Kind, resource uri.URI) error {
var ve *ValidationError
if errors.As(err, &ve) {
return ve
}
return NewError(&ValidationError{
Kind: kind,
Err: err,
URI: resource,
}, resource)
}
func (e *ValidationError) Error() string {
return fmt.Sprintf("openapi: error validating %s %s: %s", e.Kind, e.URI, e.Err)
}
func (e *ValidationError) Unwrap() error {
return e.Err
}
type SemVerError struct {
Value string
Err error
URI uri.URI
}
func (e SemVerError) Error() string {
return fmt.Sprintf("openapi: error parsing semver %q for %s: %v", e.Value, e.URI, e.Err)
}
func (e SemVerError) Unwrap() error {
return e.Err
}
func (e SemVerError) Is(err error) bool {
if errors.Is(e.Err, err) {
return true
}
switch {
case errors.Is(err, ErrInvalidSemVer):
return errors.Is(err, semver.ErrInvalidSemVer)
case errors.Is(e.Err, ErrInvalidSemVerMetadata):
return errors.Is(err, semver.ErrInvalidMetadata)
case errors.Is(e.Err, ErrInvalidSemVerPrerelease):
return errors.Is(err, semver.ErrInvalidPrerelease)
}
return false
}
func NewSemVerError(err error, value string, uri uri.URI) error {
if err == nil {
return nil
}
var sv *SemVerError
if errors.As(err, &sv) {
return sv
}
return NewError(&SemVerError{
Value: value,
Err: translateSemVerErr(err),
URI: uri,
}, uri)
}
func translateSemVerErr(err error) error {
switch {
case err == nil:
return nil
case errors.Is(err, semver.ErrInvalidSemVer):
return ErrInvalidSemVer
case errors.Is(err, semver.ErrInvalidMetadata):
return ErrInvalidSemVerMetadata
case errors.Is(err, semver.ErrInvalidPrerelease):
return ErrInvalidSemVerPrerelease
}
return err
}
type ResolutionError struct {
URI uri.URI
Expected Kind
Actual Kind
RefType RefType
}
func (e *ResolutionError) Error() string {
return fmt.Sprintf("%v cannot resolve %s to %s for %s: %s", ErrInvalidResolution, e.Actual, e.Expected, e.RefType, e.URI)
}
func (e *ResolutionError) Unwrap() error {
return ErrInvalidResolution
}
func NewResolutionError(r Ref, expected, actual Kind) error {
return &ResolutionError{
URI: r.AbsoluteLocation(),
Actual: actual,
Expected: expected,
RefType: r.RefType(),
}
}