适配 errorx v1.2:错误值不持有 ctx,改用 errors.As + GetCode/Msg(ctx)
- response.go: errorx.As/val.Code() 换成 errors.As(errorx.ErrorInterface) 取 GetCode(),文案改用 Msg(ctx) 按请求语言渲染(errorx v1.2 起错误值只保存语言, 渲染时再传 ctx)。 - FormatMessage: langx v0.1 起未知 key 的默认码从 200 变成 -1,命中默认码时 回落到 SetDefaultSuccessCode,保持升级前 code:200 的行为。 - go.mod: github.com/yuninks/errorx v1.2.0 / github.com/yuninks/langx v0.1.0。
This commit is contained in:
+5
-5
@@ -66,8 +66,8 @@ func ExampleError() {
|
|||||||
|
|
||||||
// 实际项目中通常在单独的 errors 包里用 var 声明一次
|
// 实际项目中通常在单独的 errors 包里用 var 声明一次
|
||||||
var (
|
var (
|
||||||
ExampleErrParamsInvalid = errorx.NewCode("example_params_invalid", 40001, "参数异常")
|
ExampleErrParamsInvalid = errorx.New("example_params_invalid", 40001, "参数异常")
|
||||||
ExampleErrNotFound = errorx.NewCode("example_not_found", 40004, "资源不存在")
|
ExampleErrNotFound = errorx.New("example_not_found", 40004, "资源不存在")
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleError_businessCode() {
|
func ExampleError_businessCode() {
|
||||||
@@ -81,7 +81,7 @@ func ExampleError_businessCode() {
|
|||||||
// 中文请求
|
// 中文请求
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
responsex.Error(context.Background(), w,
|
responsex.Error(context.Background(), w,
|
||||||
ExampleErrParamsInvalid.New(context.Background()),
|
ExampleErrParamsInvalid.Err(),
|
||||||
responsex.SetIgnoreLog(),
|
responsex.SetIgnoreLog(),
|
||||||
)
|
)
|
||||||
fmt.Println("zh:", w.Body.String())
|
fmt.Println("zh:", w.Body.String())
|
||||||
@@ -90,7 +90,7 @@ func ExampleError_businessCode() {
|
|||||||
ctxEn := langx.SetCtxLang(context.Background(), "en")
|
ctxEn := langx.SetCtxLang(context.Background(), "en")
|
||||||
w = httptest.NewRecorder()
|
w = httptest.NewRecorder()
|
||||||
responsex.Error(ctxEn, w,
|
responsex.Error(ctxEn, w,
|
||||||
ExampleErrParamsInvalid.New(ctxEn),
|
ExampleErrParamsInvalid.Err(),
|
||||||
responsex.SetIgnoreLog(),
|
responsex.SetIgnoreLog(),
|
||||||
)
|
)
|
||||||
fmt.Println("en:", w.Body.String())
|
fmt.Println("en:", w.Body.String())
|
||||||
@@ -106,7 +106,7 @@ func ExampleErrorWithData() {
|
|||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
responsex.ErrorWithData(context.Background(), w,
|
responsex.ErrorWithData(context.Background(), w,
|
||||||
ExampleErrParamsInvalid.New(context.Background()),
|
ExampleErrParamsInvalid.Err(),
|
||||||
map[string]any{"field": "email", "reason": "格式不正确"},
|
map[string]any{"field": "email", "reason": "格式不正确"},
|
||||||
responsex.SetHttpStatusCode(http.StatusBadRequest),
|
responsex.SetHttpStatusCode(http.StatusBadRequest),
|
||||||
responsex.SetIgnoreLog(),
|
responsex.SetIgnoreLog(),
|
||||||
|
|||||||
@@ -3,6 +3,6 @@ module code.yun.ink/pkg/responsex
|
|||||||
go 1.22.4
|
go 1.22.4
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/yuninks/errorx v1.0.0
|
github.com/yuninks/errorx v1.2.0
|
||||||
github.com/yuninks/langx v0.0.7
|
github.com/yuninks/langx v0.1.0
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
github.com/yuninks/errorx v1.0.0 h1:/AS4dSEVPkfvs3hJFYgqYBFLKyVvxFV32XXbMXoHwvk=
|
github.com/yuninks/errorx v1.2.0 h1:x9ZBvx8L58mm0dHzKN7COFCQ/MTeZgeR2Gp/wWa+ZEE=
|
||||||
github.com/yuninks/errorx v1.0.0/go.mod h1:N9e4ShfCTkLjHnGShAG0x9n2SYtCDKhKDMxRmyr05hU=
|
github.com/yuninks/errorx v1.2.0/go.mod h1:5Z4rL9HiANUtonWiGZsVisqG2dyrt7c3lKKTdpyw2g0=
|
||||||
github.com/yuninks/langx v0.0.7 h1:QUt27h/FjnT7//759GoM/+p4ew+HBr10ZN/fkqeQfyA=
|
github.com/yuninks/langx v0.1.0 h1:yqflUw7a+G6i073GzpQppuA/ftPtDh9Azlcr4OtetFE=
|
||||||
github.com/yuninks/langx v0.0.7/go.mod h1:nAylzjNIjCThhEQSJsIKP8Vhja0aJlcyig+NsNPZLSk=
|
github.com/yuninks/langx v0.1.0/go.mod h1:nAylzjNIjCThhEQSJsIKP8Vhja0aJlcyig+NsNPZLSk=
|
||||||
|
|||||||
+16
-4
@@ -6,6 +6,7 @@ package responsex
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/yuninks/errorx"
|
"github.com/yuninks/errorx"
|
||||||
@@ -58,6 +59,12 @@ func resolveSuccessMessage(ctx context.Context) string {
|
|||||||
// Msg格式化响应
|
// Msg格式化响应
|
||||||
func FormatMessage(ctx context.Context, w http.ResponseWriter, msgKey string, format map[string]string, data interface{}, ops ...Option) {
|
func FormatMessage(ctx context.Context, w http.ResponseWriter, msgKey string, format map[string]string, data interface{}, ops ...Option) {
|
||||||
code, msg := langx.GetTransFormatCtx(ctx, msgKey, format)
|
code, msg := langx.GetTransFormatCtx(ctx, msgKey, format)
|
||||||
|
// langx v0.1 起「未知 key」的默认码是 -1(v0.0.x 是 200)。responsex 的成功码
|
||||||
|
// 由 SetDefaultSuccessCode 决定,所以把默认码视为「没有业务码」,回落到它,
|
||||||
|
// 保持与升级前一致的 code:200。
|
||||||
|
if code == langx.GetDefaultCode() {
|
||||||
|
code = op.defaultSuccessCode
|
||||||
|
}
|
||||||
baseOps := []Option{SetCode(code), SetMessage(msg), SetData(data)}
|
baseOps := []Option{SetCode(code), SetMessage(msg), SetData(data)}
|
||||||
baseOps = append(baseOps, ops...)
|
baseOps = append(baseOps, ops...)
|
||||||
ResponseCtx(ctx, w, baseOps...)
|
ResponseCtx(ctx, w, baseOps...)
|
||||||
@@ -85,12 +92,17 @@ func ErrorWithData(ctx context.Context, w http.ResponseWriter, err error, data i
|
|||||||
code := op.defaultErrorCode
|
code := op.defaultErrorCode
|
||||||
msg := "请求失败"
|
msg := "请求失败"
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if val, ok := errorx.As(err); ok {
|
// errorx v1.2 起错误值不再持有 context:错误常量只负责定义与构造,
|
||||||
if langx.GetDefaultCode() != val.Code() {
|
// 文案要在出口用 Msg(ctx) 按请求语言渲染,业务码用 GetCode() 取。
|
||||||
code = val.Code()
|
var ei errorx.ErrorInterface
|
||||||
|
if errors.As(err, &ei) {
|
||||||
|
if bizCode := ei.GetCode(); langx.GetDefaultCode() != bizCode {
|
||||||
|
code = bizCode
|
||||||
}
|
}
|
||||||
|
msg = ei.Msg(ctx)
|
||||||
|
} else {
|
||||||
|
msg = err.Error()
|
||||||
}
|
}
|
||||||
msg = err.Error()
|
|
||||||
}
|
}
|
||||||
baseOps := []Option{SetCode(code), SetMessage(msg), SetData(data)}
|
baseOps := []Option{SetCode(code), SetMessage(msg), SetData(data)}
|
||||||
baseOps = append(baseOps, ops...)
|
baseOps = append(baseOps, ops...)
|
||||||
|
|||||||
Reference in New Issue
Block a user