11 Star 11 Fork 0

Gitee 极速下载/goa

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/goadesign/goa
克隆/下载
http_error.go 2.18 KB
一键复制 编辑 原始数据 按行查看 历史
Nitin 提交于 2018-12-06 13:43 . [BREAKING] dsl package refactor (#1913)
package expr
import (
"goa.design/goa/eval"
)
type (
// HTTPErrorExpr defines a HTTP error response including its name,
// status, headers and result type.
HTTPErrorExpr struct {
// ErrorExpr is the underlying goa design error expression.
*ErrorExpr
// Name of error, we need a separate copy of the name to match it
// up with the appropriate ErrorExpr.
Name string
// Response is the corresponding HTTP response.
Response *HTTPResponseExpr
}
)
// EvalName returns the generic definition name used in error messages.
func (e *HTTPErrorExpr) EvalName() string {
return "HTTP error " + e.Name
}
// Validate makes sure there is a error expression that matches the HTTP error
// expression.
func (e *HTTPErrorExpr) Validate() *eval.ValidationErrors {
verr := new(eval.ValidationErrors)
switch p := e.Response.Parent.(type) {
case *HTTPEndpointExpr:
if p.MethodExpr.Error(e.Name) == nil {
verr.Add(e, "Error %#v does not match an error defined in the method", e.Name)
}
case *HTTPServiceExpr:
if p.Error(e.Name) == nil {
verr.Add(e, "Error %#v does not match an error defined in the service", e.Name)
}
case *RootExpr:
if Root.Error(e.Name) == nil {
verr.Add(e, "Error %#v does not match an error defined in the API", e.Name)
}
}
return verr
}
// Finalize looks up the corresponding method error expression.
func (e *HTTPErrorExpr) Finalize(a *HTTPEndpointExpr) {
var ee *ErrorExpr
switch p := e.Response.Parent.(type) {
case *HTTPEndpointExpr:
ee = p.MethodExpr.Error(e.Name)
case *HTTPServiceExpr:
ee = p.Error(e.Name)
case *RootExpr:
ee = Root.Error(e.Name)
}
e.ErrorExpr = ee
e.Response.Finalize(a, e.AttributeExpr)
if e.Response.Body == nil {
e.Response.Body = httpErrorResponseBody(a, e)
}
// Initialize response content type if result is media type.
if e.Response.Body.Type == Empty {
return
}
if e.Response.ContentType != "" {
return
}
mt, ok := e.Response.Body.Type.(*ResultTypeExpr)
if !ok {
return
}
e.Response.ContentType = mt.Identifier
}
// Dup creates a copy of the error expression.
func (e *HTTPErrorExpr) Dup() *HTTPErrorExpr {
return &HTTPErrorExpr{
ErrorExpr: e.ErrorExpr,
Name: e.Name,
Response: e.Response.Dup(),
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/goa.git
git@gitee.com:mirrors/goa.git
mirrors
goa
goa
v2.0.0

搜索帮助