/
error.go
277 lines (269 loc) Β· 7.87 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
package dsl
import (
"goa.design/goa/v3/eval"
"goa.design/goa/v3/expr"
pkg "goa.design/goa/v3/pkg"
)
const (
// The constants below make it possible for the service specific code to
// return error names that are consistent with the names used by the
// generated request and response payload validation code.
//
// Usage:
//
// var _ = Service("divider", func() {
// Error(MissingField)
// Error(InvalidRange)
//
// Payload(func() {
// Attribute("numerator", Int)
// Attribute("denominator", Int, func() {
// Minimum(1)
// })
// Required("numerator", "denominator")
// })
//
// HTTP(func() {
// Response(MissingField, StatusBadRequest)
// Response(InvalidRange, StatusBadRequest)
// })
//
// GRPC(func() {
// Response(MissingField, CodeInvalidArgument)
// Response(InvalidRange, CodeInvalidArgument)
// })
// })
// InvalidFieldType is the error name for invalid field type errors.
InvalidFieldType = pkg.InvalidFieldType
// MissingField is the error name for missing field errors.
MissingField = pkg.MissingField
// InvalidEnumValue is the error name for invalid enum value errors.
InvalidEnumValue = pkg.InvalidEnumValue
// InvalidFormat is the error name for invalid format errors.
InvalidFormat = pkg.InvalidFormat
// InvalidPattern is the error name for invalid pattern errors.
InvalidPattern = pkg.InvalidPattern
// InvalidRange is the error name for invalid range errors.
InvalidRange = pkg.InvalidRange
// InvalidLength is the error name for invalid length errors.
InvalidLength = pkg.InvalidLength
)
// Error describes a method error return value. The description includes a
// unique name (in the scope of the method), an optional type, description and
// DSL that further describes the type. If no type is specified then the
// built-in ErrorResult type is used. The DSL syntax is identical to the
// Attribute DSL.
//
// Error must appear in the Service (to define error responses that apply to all
// the service methods) or Method expressions. Error may also appear under the API
// expression to create reusable error definitions.
//
// See Attribute for details on the Error arguments.
//
// Example:
//
// var _ = API("calc", func() {
// Error("invalid_argument") // Uses type ErrorResult
// HTTP(func() {
// Response("invalid_argument", StatusBadRequest)
// })
// })
//
// var _ = Service("divider", func() {
// Error("invalid_arguments") // Refers to error defined above.
// // No need to define HTTP mapping again.
//
// // Method which uses the default type for its response.
// Method("divide", func() {
// Payload(DivideRequest)
// Error("div_by_zero", DivByZero, "Division by zero")
// })
// })
//
func Error(name string, args ...interface{}) {
if len(args) == 0 {
args = []interface{}{expr.ErrorResult}
}
dt, desc, fn := parseAttributeArgs(nil, args...)
att := &expr.AttributeExpr{
Description: desc,
Type: dt,
}
if fn != nil {
eval.Execute(fn, att)
}
if att.Type == nil {
att.Type = expr.ErrorResult
}
erro := &expr.ErrorExpr{AttributeExpr: att, Name: name}
switch actual := eval.Current().(type) {
case *expr.APIExpr:
expr.Root.Errors = append(expr.Root.Errors, erro)
case *expr.ServiceExpr:
actual.Errors = append(actual.Errors, erro)
case *expr.MethodExpr:
actual.Errors = append(actual.Errors, erro)
default:
eval.IncompatibleDSL()
}
}
// ErrorName identifies the attribute of a custom error type used to select the
// returned error response when multiple errors of that type are defined on the
// same method. The value of the field identifies the error name as defined in
// the design. This makes it possible to define distinct transport mappings for
// the various errors (for example to return different HTTP status codes). There
// must be one and exactly one attribute defined with ErrorName on types used to
// define errors.
//
// ErrorName must appear in a Type or ResultType expression.
//
// ErrorName takes the same arguments as Attribute or Field.
//
// Example design:
//
// // All the methods exposed by service MyService can return the errors
// // "internal_error" and "bad_request". Both errors have the same type
// // CustomErrorType. "internal_error" is mapped to HTTP status 500 and
// // "bad_request" is mapped to HTTP status 400.
// var _ = Service("MyService", func() {
// Error("internal_error", CustomErrorType)
// Error("bad_request", CustomErrorType)
// HTTP(func() {
// Response("internal_error", StatusInternalServerError)
// Response("bad_request", StatusBadRequest)
// })
//
// Method("Method", func() {
// Payload(String)
// HTTP(func() {
// GET("/")
// })
// })
// })
//
// var CustomErrorType = Type("CustomError", func() {
// // The "name" attribute is used to select the error response.
// // name should be set to either "internal_error" or "bad_request" by
// // the service method returning the error.
// ErrorName("name", String, "Name of error.")
// Attribute("message", String, "Message of error.")
// Attribute("occurred_at", String, "Time error occurred.", func() {
// Format(FormatDateTime)
// })
// Required("name", "message", "occurred_at")
// })
//
// Example usage:
//
// func (s *svc) Method(ctx context.Context, p string) error {
// // ...
// if err != nil {
// return &myservice.CustomError{
// Name: "internal_error", // HTTP response status is 500.
// Message: "Something went wrong",
// OccurredAt: time.Now().Format(time.RFC3339),
// }
// }
// // ...
// return nil
// }
//
func ErrorName(args ...interface{}) {
if len(args) == 0 {
eval.IncompatibleDSL()
return
}
dsl, ok := args[len(args)-1].(func())
if ok {
args[len(args)-1] = func() {
dsl()
Meta("struct:error:name")
}
} else {
args = append(args, func() {
Meta("struct:error:name")
})
}
switch actual := args[0].(type) {
case string:
Attribute(actual, args[1:]...)
case int:
if len(args) == 1 {
eval.IncompatibleDSL()
return
}
name, ok := args[1].(string)
if !ok {
eval.InvalidArgError("name", args[1])
return
}
Field(actual, name, args[2:]...)
default:
eval.InvalidArgError("name or position", args[0])
}
}
// Temporary qualifies an error type as describing temporary (i.e. retryable)
// errors.
//
// Temporary must appear in a Error expression.
//
// Temporary takes no argument.
//
// Example:
//
// var _ = Service("divider", func() {
// Error("request_timeout", func() {
// Temporary()
// })
// })
func Temporary() {
attr, ok := eval.Current().(*expr.AttributeExpr)
if !ok {
eval.IncompatibleDSL()
return
}
attr.AddMeta("goa:error:temporary")
}
// Timeout qualifies an error type as describing errors due to timeouts.
//
// Timeout must appear in a Error expression.
//
// Timeout takes no argument.
//
// Example:
//
// var _ = Service("divider", func() {
// Error("request_timeout", func() {
// Timeout()
// })
// })
func Timeout() {
attr, ok := eval.Current().(*expr.AttributeExpr)
if !ok {
eval.IncompatibleDSL()
return
}
attr.AddMeta("goa:error:timeout")
}
// Fault qualifies an error type as describing errors due to a server-side
// fault.
//
// Fault must appear in a Error expression.
//
// Fault takes no argument.
//
// Example:
//
// var _ = Service("divider", func() {
// Error("internal_error", func() {
// Fault()
// })
// })
func Fault() {
attr, ok := eval.Current().(*expr.AttributeExpr)
if !ok {
eval.IncompatibleDSL()
return
}
attr.AddMeta("goa:error:fault")
}