/
error.go
321 lines (271 loc) · 7.5 KB
/
error.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package compile
import (
"fmt"
"strings"
"go.uber.org/thriftrw/ast"
)
// fileReadError is raised when there's an error reading a file.
type fileReadError struct {
Path string
Reason error
}
func (e fileReadError) Error() string {
return fmt.Sprintf("could not read file %q: %v", e.Path, e.Reason)
}
// parseError is raised when there's an error parsing a Thrift file.
type parseError struct {
Path string
Reason error
}
func (e parseError) Error() string {
return fmt.Sprintf("could not parse file %q: %v", e.Path, e.Reason)
}
type fileCompileError struct {
Path string
Reason error
}
func (e fileCompileError) Error() string {
return fmt.Sprintf("could not compile file %q: %v", e.Path, e.Reason)
}
// includeAsDisabledError is raised when the user attempts to use the include-as
// syntax without explicitly enabling it.
type includeAsDisabledError struct{}
func (e includeAsDisabledError) Error() string {
return "include-as syntax is currently disabled"
}
// includeError is raised when there is an error including another Thrift
// file.
type includeError struct {
Include *ast.Include
Reason error
}
func (e includeError) Error() string {
return fmt.Sprintf(
"cannot include %q as %q on line %d: %v",
e.Include.Path, e.Include.Name, e.Include.Line, e.Reason,
)
}
// definitionError is raised when there was an error compiling a definition
// from the Thrift file.
type definitionError struct {
Definition ast.Definition
Reason error
}
func (e definitionError) Error() string {
return fmt.Sprintf(
"cannot define %q on line %d: %v",
e.Definition.Info().Name, e.Definition.Info().Line, e.Reason,
)
}
// compileError is a general error raised while trying to compile components
// of the Thrift file.
type compileError struct {
Target string
Line int
Reason error
}
func (e compileError) Error() string {
msg := fmt.Sprintf("cannot compile %q", e.Target)
if e.Line > 0 {
msg += fmt.Sprintf(" on line %d", e.Line)
}
if e.Reason != nil {
msg += fmt.Sprintf(": %v", e.Reason)
}
return msg
}
// referenceError is raised when there's an error resolving a reference.
type referenceError struct {
Target string
Line int
ScopeName string
Reason error
}
func (e referenceError) Error() string {
msg := fmt.Sprintf("could not resolve reference %q", e.Target)
if e.Line > 0 {
msg += fmt.Sprintf(" on line %d", e.Line)
}
if len(e.ScopeName) > 0 {
msg += fmt.Sprintf(" in %q", e.ScopeName)
}
if e.Reason != nil {
msg += fmt.Sprintf(": %v", e.Reason)
}
return msg
}
type unrecognizedModuleError struct {
Name string
Reason error
}
func (e unrecognizedModuleError) Error() string {
msg := fmt.Sprintf("unknown module %q", e.Name)
if e.Reason != nil {
msg += fmt.Sprintf(": %v", e.Reason)
}
return msg
}
type unrecognizedEnumItemError struct {
EnumName string
ItemName string
}
func (e unrecognizedEnumItemError) Error() string {
return fmt.Sprintf(
"enum %q does not have an item named %q", e.EnumName, e.ItemName,
)
}
// lookupError is raised by Module if the Lookup* functions are called with
// unknown values.
type lookupError struct {
Name string
}
func (e lookupError) Error() string {
return fmt.Sprintf("unknown identifier %q", e.Name)
}
type requirednessRequiredError struct {
FieldName string
Line int
}
func (e requirednessRequiredError) Error() string {
return fmt.Sprintf(
"field %q on line %d is not marked required or optional",
e.FieldName, e.Line,
)
}
type cannotBeRequiredError struct {
FieldName string
Line int
}
func (e cannotBeRequiredError) Error() string {
return fmt.Sprintf(
"field %q on line %d is marked as required but it cannot be required",
e.FieldName, e.Line,
)
}
type defaultValueNotAllowedError struct {
FieldName string
Line int
}
func (e defaultValueNotAllowedError) Error() string {
return fmt.Sprintf(
"field %q on line %d cannot have a default value", e.FieldName, e.Line,
)
}
type fieldIDConflictError struct {
ID int16
Name string
}
func (e fieldIDConflictError) Error() string {
return fmt.Sprintf("field %q has already used ID %d", e.Name, e.ID)
}
type oneWayCannotReturnError struct {
Name string
}
func (e oneWayCannotReturnError) Error() string {
return fmt.Sprintf(
"function %q cannot return values or raise exceptions: %q is oneway",
e.Name, e.Name,
)
}
type notAnExceptionError struct {
TypeName string
FieldName string
}
func (e notAnExceptionError) Error() string {
return fmt.Sprintf(
"field %q with type %q is not an exception", e.FieldName, e.TypeName,
)
}
type typeReferenceCycleError struct {
Nodes []TypeSpec
}
func (e typeReferenceCycleError) Error() string {
// Outputs:
//
// found a type reference cycle:
// foo (a.thrift)
// -> bar (b.thrift)
// -> foo (a.thrift)
//
// File names are omitted if all types are from the same file.
files := make(map[string]struct{})
for _, t := range e.Nodes {
file := t.ThriftFile()
if file != "" {
files[t.ThriftFile()] = struct{}{}
}
}
includeFileName := len(files) > 1
lines := make([]string, 0, len(e.Nodes)+1)
lines = append(lines, "found a type reference cycle:")
for i, t := range e.Nodes {
line := " "
if i == 0 {
line += " "
} else {
line += "-> "
}
file := t.ThriftFile()
if file != "" && includeFileName {
line += fmt.Sprintf("%v (%v)", t.ThriftName(), file)
} else {
line += t.ThriftName()
}
lines = append(lines, line)
}
return strings.Join(lines, "\n")
}
// Failure to cast a Constantvalue to a specific type.
type constantValueCastError struct {
Value ConstantValue
Type TypeSpec
Reason error // optional
}
func (e constantValueCastError) Error() string {
s := fmt.Sprintf("cannot cast %v to %q", e.Value, e.Type.ThriftName())
if e.Reason != nil {
s += fmt.Sprintf(": %v", e.Reason)
}
return s
}
// Failure to cast a specific field of a struct literal.
type constantStructFieldCastError struct {
FieldName string
Reason error
}
func (e constantStructFieldCastError) Error() string {
return fmt.Sprintf("failed to cast field %q: %v", e.FieldName, e.Reason)
}
// Failure to cast a value referenced by a named constant.
type constantCastError struct {
Name string
Reason error
}
func (e constantCastError) Error() string {
return fmt.Sprintf("failed to cast constant %q: %v", e.Name, e.Reason)
}
type annotationConflictError struct {
Reason error
}
func (e annotationConflictError) Error() string {
return fmt.Sprintf("annotation conflict: %v", e.Reason)
}