package zerox import ( "context" "git.gzquan.cn/pkg/go-common/xerr" "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/rest/httpx" "net/http" ) // BaseResp 基础返回结构 type BaseResp struct { Code int `json:"code"` Msg string `json:"message"` Data any `json:"data"` } // SetOkHandler 成功处理方法 func SetOkHandler() { httpx.SetOkHandler(func(ctx context.Context, a any) any { resp := &BaseResp{ Code: http.StatusOK, Msg: "success", Data: a, } return resp }) } // SetErrorHandler 失败处理方法 func SetErrorHandler() { httpx.SetErrorHandler(func(err error) (int, any) { logx.Errorf("error type is %T, value: %v", err, err) switch e := err.(type) { case *xerr.CodeMsg: return http.StatusOK, &BaseResp{ Code: e.GetErrCode(), Msg: e.GetErrMsg(), Data: nil, } case *xerr.ServerMsg: return http.StatusInternalServerError, &BaseResp{ Code: e.GetErrCode(), Msg: e.GetErrMsg(), Data: nil, } default: return http.StatusServiceUnavailable, &BaseResp{ Code: xerr.BaseError, Msg: err.Error(), Data: nil, } } }) }