-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy patherror.go
132 lines (127 loc) Β· 3.06 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
package dsl
import (
"goa.design/goa/eval"
"goa.design/goa/expr"
)
// 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.
//
// See Attribute for details on the Error arguments.
//
// Example:
//
// var _ = Service("divider", func() {
// Error("invalid_arguments") // Uses type ErrorResult
//
// // 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.ServiceExpr:
actual.Errors = append(actual.Errors, erro)
case *expr.MethodExpr:
actual.Errors = append(actual.Errors, erro)
default:
eval.IncompatibleDSL()
}
}
// 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
}
if attr.Meta == nil {
attr.Meta = make(expr.MetaExpr)
}
attr.Meta["goa:error:temporary"] = nil
}
// 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
}
if attr.Meta == nil {
attr.Meta = make(expr.MetaExpr)
}
attr.Meta["goa:error:timeout"] = nil
}
// 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
}
if attr.Meta == nil {
attr.Meta = make(expr.MetaExpr)
}
attr.Meta["goa:error:fault"] = nil
}