优化默认返回码
This commit is contained in:
+12
-6
@@ -56,29 +56,35 @@ func FormatMessage(ctx context.Context, w http.ResponseWriter, message string, f
|
||||
|
||||
// 成功的响应
|
||||
func Success(ctx context.Context, w http.ResponseWriter, data interface{}, info ...interface{}) {
|
||||
ResponseCtx(ctx, w, 200, "请求成功", data, info)
|
||||
ResponseCtx(ctx, w, op.defaultSuccessCode, "请求成功", data, info)
|
||||
}
|
||||
|
||||
func SuccessWithPage(ctx context.Context, w http.ResponseWriter, data interface{}, page pagination) {
|
||||
ResponseCtx(ctx, w, 200, "请求成功", data, page)
|
||||
ResponseCtx(ctx, w, op.defaultSuccessCode, "请求成功", data, page)
|
||||
}
|
||||
|
||||
// 失败的响应
|
||||
func Error(ctx context.Context, w http.ResponseWriter, err error) {
|
||||
code := 400
|
||||
code := op.defaultErrorCode
|
||||
msg := "请求失败"
|
||||
if err != nil {
|
||||
val, ok := err.(*langx.LangError)
|
||||
if ok {
|
||||
code = val.Code()
|
||||
msg = err.Error()
|
||||
if langx.GetDefaultCode() != val.Code() {
|
||||
code = val.Code()
|
||||
}
|
||||
}
|
||||
msg = err.Error()
|
||||
}
|
||||
ResponseCtx(ctx, w, code, msg, "")
|
||||
}
|
||||
|
||||
func ErrorStr(ctx context.Context, w http.ResponseWriter, msg string) {
|
||||
code, msg := langx.GetTrans("zh-CN", msg, nil)
|
||||
msg = langx.GetMsgCtx(ctx, msg)
|
||||
code := langx.GetCode(msg)
|
||||
if code == langx.GetDefaultCode() {
|
||||
code = op.defaultErrorCode
|
||||
}
|
||||
ResponseCtx(ctx, w, code, msg, "")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user