35 lines
1.3 KiB
Go
35 lines
1.3 KiB
Go
package svc
|
|
|
|
import (
|
|
omni "git.yuntuo.hk/k2safe/go-omni"
|
|
omnierrors "git.yuntuo.hk/k2safe/go-omni/errors"
|
|
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
|
)
|
|
|
|
func (c *ServiceContext) AppOptions() []omni.Option {
|
|
return []omni.Option{omni.WithErrors(omnierrors.Catalog{
|
|
DefaultStatus: 400,
|
|
Definitions: map[string]omnierrors.Definition{
|
|
"INVALID": {Code: 400002, Message: "用户名格式不正确"},
|
|
"TOO_LONG": {Code: 400003, Message: "{label}不能超过{max}个字符", Params: []omnierrors.Parameter{{Name: "label", Type: "string"}, {Name: "max", Type: "int"}}},
|
|
"INTERNAL_ERROR": {Status: 500, Code: 9999, Message: "系统错误"},
|
|
"NOT_FOUND": {Status: 404, Code: 404001, Message: "接口不存在"},
|
|
"METHOD_NOT_ALLOWED": {Status: 405, Code: 405001, Message: "请求方法不支持"},
|
|
"BODY_TOO_LARGE": {Status: 413, Code: 413001, Message: "请求体太大"},
|
|
"INVALID_MULTIPART": {Status: 400, Code: 400004, Message: "上传内容不完整"},
|
|
},
|
|
})}
|
|
}
|
|
func (c *ServiceContext) GlobalMiddleware() []httpx.Middleware {
|
|
return []httpx.Middleware{
|
|
func(next httpx.Handler) httpx.Handler {
|
|
return func(c *httpx.Context) error {
|
|
if c.Header("X-Fail") != "" {
|
|
return omnierrors.ByCode("INVALID")
|
|
}
|
|
return next(c)
|
|
}
|
|
},
|
|
}
|
|
}
|