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 DS
(name string, args ...any)
| 84 | // }) |
| 85 | // }) |
| 86 | func Error(name string, args ...any) { |
| 87 | if len(args) == 0 { |
| 88 | args = []any{expr.ErrorResult} |
| 89 | } |
| 90 | dt, desc, fn := parseAttributeArgs(nil, args...) |
| 91 | att := &expr.AttributeExpr{ |
| 92 | Description: desc, |
| 93 | Type: dt, |
| 94 | } |
| 95 | if fn != nil { |
| 96 | eval.Execute(fn, att) |
| 97 | } |
| 98 | if att.Type == nil { |
| 99 | att.Type = expr.ErrorResult |
| 100 | } |
| 101 | erro := &expr.ErrorExpr{AttributeExpr: att, Name: name} |
| 102 | switch actual := eval.Current().(type) { |
| 103 | case *expr.APIExpr: |
| 104 | expr.Root.Errors = append(expr.Root.Errors, erro) |
| 105 | case *expr.ServiceExpr: |
| 106 | actual.Errors = append(actual.Errors, erro) |
| 107 | case *expr.MethodExpr: |
| 108 | actual.Errors = append(actual.Errors, erro) |
| 109 | default: |
| 110 | eval.IncompatibleDSL() |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // ErrorName identifies the attribute of a custom error type used to select the |
| 115 | // returned error response when multiple errors of that type are defined on the |