适配 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 声明一次
|
||||
var (
|
||||
ExampleErrParamsInvalid = errorx.NewCode("example_params_invalid", 40001, "参数异常")
|
||||
ExampleErrNotFound = errorx.NewCode("example_not_found", 40004, "资源不存在")
|
||||
ExampleErrParamsInvalid = errorx.New("example_params_invalid", 40001, "参数异常")
|
||||
ExampleErrNotFound = errorx.New("example_not_found", 40004, "资源不存在")
|
||||
)
|
||||
|
||||
func ExampleError_businessCode() {
|
||||
@@ -81,7 +81,7 @@ func ExampleError_businessCode() {
|
||||
// 中文请求
|
||||
w := httptest.NewRecorder()
|
||||
responsex.Error(context.Background(), w,
|
||||
ExampleErrParamsInvalid.New(context.Background()),
|
||||
ExampleErrParamsInvalid.Err(),
|
||||
responsex.SetIgnoreLog(),
|
||||
)
|
||||
fmt.Println("zh:", w.Body.String())
|
||||
@@ -90,7 +90,7 @@ func ExampleError_businessCode() {
|
||||
ctxEn := langx.SetCtxLang(context.Background(), "en")
|
||||
w = httptest.NewRecorder()
|
||||
responsex.Error(ctxEn, w,
|
||||
ExampleErrParamsInvalid.New(ctxEn),
|
||||
ExampleErrParamsInvalid.Err(),
|
||||
responsex.SetIgnoreLog(),
|
||||
)
|
||||
fmt.Println("en:", w.Body.String())
|
||||
@@ -106,7 +106,7 @@ func ExampleErrorWithData() {
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
responsex.ErrorWithData(context.Background(), w,
|
||||
ExampleErrParamsInvalid.New(context.Background()),
|
||||
ExampleErrParamsInvalid.Err(),
|
||||
map[string]any{"field": "email", "reason": "格式不正确"},
|
||||
responsex.SetHttpStatusCode(http.StatusBadRequest),
|
||||
responsex.SetIgnoreLog(),
|
||||
|
||||
Reference in New Issue
Block a user