优化默认返回码

This commit is contained in:
Yun
2024-07-10 17:32:12 +08:00
parent f092f7e49a
commit d05963c86a
4 changed files with 38 additions and 19 deletions
+12 -6
View File
@@ -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, "")
}