自定义错误
This commit is contained in:
parent
0ed61a0f2c
commit
b29c603199
|
|
@ -88,8 +88,6 @@ message CmbQueryReply {
|
||||||
string orgNo = 12 [json_name = "orgNo"];
|
string orgNo = 12 [json_name = "orgNo"];
|
||||||
// 扩展字段
|
// 扩展字段
|
||||||
string ext = 13 [json_name = "ext"];
|
string ext = 13 [json_name = "ext"];
|
||||||
// 错误码
|
|
||||||
string thirdErrCode = 3 [json_name = "thirdErrCode"];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -34,7 +34,10 @@ func (v *VoucherBiz) order(ctx context.Context, req *bo.OrderCreateReqBo, produc
|
||||||
// 真实发放
|
// 真实发放
|
||||||
voucherNo, err = v.WechatCpnRepo.Order(ctx, order)
|
voucherNo, err = v.WechatCpnRepo.Order(ctx, order)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, v.fail(ctx, order, err.Error())
|
if err2 := v.fail(ctx, order, err.Error()); err2 != nil {
|
||||||
|
return nil, err2
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ type WechatCpnRepo interface {
|
||||||
Order(ctx context.Context, order *bo.OrderBo) (couponId string, err error)
|
Order(ctx context.Context, order *bo.OrderBo) (couponId string, err error)
|
||||||
Query(ctx context.Context, order *bo.OrderBo) (vo.OrderStatus, error)
|
Query(ctx context.Context, order *bo.OrderBo) (vo.OrderStatus, error)
|
||||||
QueryProduct(ctx context.Context, stockCreatorMchId, stockId string) (*cashcoupons.Stock, error)
|
QueryProduct(ctx context.Context, stockCreatorMchId, stockId string) (*cashcoupons.Stock, error)
|
||||||
QueryCallback(ctx context.Context, mchId, mchCertNo string) (*cashcoupons.Callback, error)
|
QueryCallback(ctx context.Context) (*cashcoupons.Callback, error)
|
||||||
SetCallback(ctx context.Context, mchId, mchCertNo, url string) (*cashcoupons.SetCallbackResponse, error)
|
SetCallback(ctx context.Context, url string) (*cashcoupons.SetCallbackResponse, error)
|
||||||
RegisterNotifyTag(ctx context.Context, stockID string) error
|
RegisterNotifyTag(ctx context.Context, stockID string) error
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons"
|
"github.com/wechatpay-apiv3/wechatpay-go/services/cashcoupons"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
err2 "voucher/api/err"
|
||||||
"voucher/internal/biz/bo"
|
"voucher/internal/biz/bo"
|
||||||
"voucher/internal/biz/vo"
|
"voucher/internal/biz/vo"
|
||||||
"voucher/internal/biz/wechatrepo"
|
"voucher/internal/biz/wechatrepo"
|
||||||
|
|
@ -35,6 +36,33 @@ func NewCpnRepoImpl(bc *conf.Bootstrap) (wechatrepo.WechatCpnRepo, error) {
|
||||||
return &CpnRepoImpl{bc: bc, Server: server}, nil
|
return &CpnRepoImpl{bc: bc, Server: server}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *CpnRepoImpl) GetClient(ctx context.Context) (*core.Client, error) {
|
||||||
|
|
||||||
|
client, err := data.GetClient(ctx, c.Server)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err2.ErrorWechatFAIL(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return client, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *CpnRepoImpl) bodyErr(_ context.Context, result *core.APIResult) error {
|
||||||
|
|
||||||
|
bodyBytes, err := io.ReadAll(result.Response.Body)
|
||||||
|
if err != nil {
|
||||||
|
return err2.ErrorWechatFAIL(fmt.Sprintf("读取微信错误返回body报错:%s", err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Errorf("请求微信返回错误:%s", string(bodyBytes))
|
||||||
|
|
||||||
|
var errBody ErrBody
|
||||||
|
if err = json.Unmarshal(bodyBytes, &errBody); err != nil {
|
||||||
|
return err2.ErrorWechatFAIL(fmt.Sprintf("微信错误返回内容解析错误:%s", err.Error()))
|
||||||
|
}
|
||||||
|
|
||||||
|
return errBody.GetWechatError()
|
||||||
|
}
|
||||||
|
|
||||||
func (c *CpnRepoImpl) Order(ctx context.Context, order *bo.OrderBo) (string, error) {
|
func (c *CpnRepoImpl) Order(ctx context.Context, order *bo.OrderBo) (string, error) {
|
||||||
|
|
||||||
req := cashcoupons.SendCouponRequest{
|
req := cashcoupons.SendCouponRequest{
|
||||||
|
|
@ -46,7 +74,7 @@ func (c *CpnRepoImpl) Order(ctx context.Context, order *bo.OrderBo) (string, err
|
||||||
StockCreatorMchid: core.String(order.MerchantNo),
|
StockCreatorMchid: core.String(order.MerchantNo),
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := data.GetClient(ctx, c.Server)
|
client, err := c.GetClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
@ -57,23 +85,7 @@ func (c *CpnRepoImpl) Order(ctx context.Context, order *bo.OrderBo) (string, err
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
bodyBytes, err := io.ReadAll(result.Response.Body)
|
return "", c.bodyErr(ctx, result)
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Errorf("请求微信返回错误:%s", string(bodyBytes))
|
|
||||||
|
|
||||||
var errBody ErrBody
|
|
||||||
if err = json.Unmarshal(bodyBytes, &errBody); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", errBody.GetWechatError()
|
|
||||||
}
|
|
||||||
|
|
||||||
if result.Response.StatusCode != CodeSuccess {
|
|
||||||
return "", fmt.Errorf("请求错误,code:%s", result.Response.Status)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return *resp.CouponId, nil
|
return *resp.CouponId, nil
|
||||||
|
|
@ -87,7 +99,7 @@ func (c *CpnRepoImpl) Query(ctx context.Context, orderWechat *bo.OrderBo) (vo.Or
|
||||||
Openid: core.String(orderWechat.Account),
|
Openid: core.String(orderWechat.Account),
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := data.GetClient(ctx, c.Server)
|
client, err := c.GetClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
@ -97,24 +109,7 @@ func (c *CpnRepoImpl) Query(ctx context.Context, orderWechat *bo.OrderBo) (vo.Or
|
||||||
resp, result, err := svc.QueryCoupon(ctx, req)
|
resp, result, err := svc.QueryCoupon(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
bodyBytes, err := io.ReadAll(result.Response.Body)
|
return 0, c.bodyErr(ctx, result)
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Errorf("请求微信返回错误:%s", string(bodyBytes))
|
|
||||||
|
|
||||||
var errBody ErrBody
|
|
||||||
if err = json.Unmarshal(bodyBytes, &errBody); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0, errBody.GetWechatError()
|
|
||||||
}
|
|
||||||
|
|
||||||
if result.Response.StatusCode != CodeSuccess {
|
|
||||||
|
|
||||||
return 0, fmt.Errorf("微信返回错误 Response StatusCode[%d]", result.Response.StatusCode)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return CpnStatus(*resp.Status).GetStatus()
|
return CpnStatus(*resp.Status).GetStatus()
|
||||||
|
|
@ -122,7 +117,7 @@ func (c *CpnRepoImpl) Query(ctx context.Context, orderWechat *bo.OrderBo) (vo.Or
|
||||||
|
|
||||||
func (c *CpnRepoImpl) QueryProduct(ctx context.Context, stockCreatorMchId, stockId string) (*cashcoupons.Stock, error) {
|
func (c *CpnRepoImpl) QueryProduct(ctx context.Context, stockCreatorMchId, stockId string) (*cashcoupons.Stock, error) {
|
||||||
|
|
||||||
client, err := data.GetClient(ctx, c.Server)
|
client, err := c.GetClient(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -135,38 +130,16 @@ func (c *CpnRepoImpl) QueryProduct(ctx context.Context, stockCreatorMchId, stock
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bodyBytes, err := io.ReadAll(result.Response.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var errBody ErrBody
|
return nil, c.bodyErr(ctx, result)
|
||||||
if err = json.Unmarshal(bodyBytes, &errBody); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, errBody.GetWechatError()
|
|
||||||
}
|
|
||||||
|
|
||||||
if result.Response.StatusCode != CodeSuccess {
|
|
||||||
return nil, fmt.Errorf("查询活动微信返回错误 Response StatusCode[%d]", result.Response.StatusCode)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CpnRepoImpl) QueryCallback(ctx context.Context, mchId, mchCertNo string) (*cashcoupons.Callback, error) {
|
func (c *CpnRepoImpl) QueryCallback(ctx context.Context) (*cashcoupons.Callback, error) {
|
||||||
|
|
||||||
server := data.Server{
|
client, err := c.GetClient(ctx)
|
||||||
MchID: mchId,
|
|
||||||
MchCertificateSerialNumber: mchCertNo,
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := server.Validate(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
client, err := data.GetClient(ctx, server)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -174,45 +147,20 @@ func (c *CpnRepoImpl) QueryCallback(ctx context.Context, mchId, mchCertNo string
|
||||||
svc := cashcoupons.CallBackUrlApiService{Client: client}
|
svc := cashcoupons.CallBackUrlApiService{Client: client}
|
||||||
|
|
||||||
response, result, err := svc.QueryCallback(ctx, cashcoupons.QueryCallbackRequest{
|
response, result, err := svc.QueryCallback(ctx, cashcoupons.QueryCallbackRequest{
|
||||||
Mchid: core.String(mchId),
|
Mchid: core.String(c.bc.Wechat.MchID),
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
log.Errorf("请求微信返回错误:%s", err.Error())
|
return nil, c.bodyErr(ctx, result)
|
||||||
|
|
||||||
bodyBytes, err := io.ReadAll(result.Response.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var errBody ErrBody
|
|
||||||
if err = json.Unmarshal(bodyBytes, &errBody); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, errBody.GetWechatError()
|
|
||||||
}
|
|
||||||
|
|
||||||
if result.Response.StatusCode != CodeSuccess {
|
|
||||||
return nil, fmt.Errorf("微信返回错误tatus[%s]", result.Response.Status)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CpnRepoImpl) SetCallback(ctx context.Context, mchId, mchCertNo, url string) (*cashcoupons.SetCallbackResponse, error) {
|
func (c *CpnRepoImpl) SetCallback(ctx context.Context, url string) (*cashcoupons.SetCallbackResponse, error) {
|
||||||
|
|
||||||
server := data.Server{
|
client, err := c.GetClient(ctx)
|
||||||
MchID: mchId,
|
|
||||||
MchCertificateSerialNumber: mchCertNo,
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := server.Validate(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
client, err := data.GetClient(ctx, server)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -220,30 +168,14 @@ func (c *CpnRepoImpl) SetCallback(ctx context.Context, mchId, mchCertNo, url str
|
||||||
svc := cashcoupons.CallBackUrlApiService{Client: client}
|
svc := cashcoupons.CallBackUrlApiService{Client: client}
|
||||||
|
|
||||||
response, result, err := svc.SetCallback(ctx, cashcoupons.SetCallbackRequest{
|
response, result, err := svc.SetCallback(ctx, cashcoupons.SetCallbackRequest{
|
||||||
Mchid: core.String(mchId),
|
Mchid: core.String(c.bc.Wechat.MchID),
|
||||||
NotifyUrl: core.String(url),
|
NotifyUrl: core.String(url),
|
||||||
Switch: core.Bool(true),
|
Switch: core.Bool(true),
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
log.Errorf("请求微信返回错误:%s", err.Error())
|
return nil, c.bodyErr(ctx, result)
|
||||||
|
|
||||||
bodyBytes, err := io.ReadAll(result.Response.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var errBody ErrBody
|
|
||||||
if err = json.Unmarshal(bodyBytes, &errBody); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, fmt.Errorf("微信返回错误:%s", errBody.Message)
|
|
||||||
}
|
|
||||||
|
|
||||||
if result.Response.StatusCode != CodeSuccess {
|
|
||||||
return nil, fmt.Errorf("微信返回错误tatus[%s]", result.Response.Status)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return response, nil
|
return response, nil
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
package wechatrepoimpl
|
package wechatrepoimpl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
errors2 "github.com/go-kratos/kratos/v2/errors"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -11,3 +13,22 @@ func TestGetErrorByDescription(t *testing.T) {
|
||||||
}
|
}
|
||||||
t.Log(e.GetWechatError())
|
t.Log(e.GetWechatError())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestErr(t *testing.T) {
|
||||||
|
e := &ErrBody{
|
||||||
|
Code: "INVALID_REQUEST",
|
||||||
|
Message: "活动已结束或未激活",
|
||||||
|
}
|
||||||
|
|
||||||
|
se := errors2.FromError(e.GetWechatError())
|
||||||
|
|
||||||
|
t.Log(se.Reason)
|
||||||
|
t.Log(se.Message)
|
||||||
|
|
||||||
|
e2 := errors.New("活动已结束或未激活")
|
||||||
|
se2 := errors2.FromError(e2)
|
||||||
|
|
||||||
|
t.Log(se2.Reason)
|
||||||
|
t.Log(len(se2.Reason))
|
||||||
|
t.Log(se2.Message)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,10 @@ func (s *VoucherService) CmbOrder(ctx http.Context) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
se := errors.FromError(err)
|
se := errors.FromError(err)
|
||||||
|
|
||||||
|
if len(se.Reason) == 0 {
|
||||||
|
se.Reason = err2.CmbErr_CMB_UNKNOWN.String()
|
||||||
|
}
|
||||||
|
|
||||||
bizReply = &v1.CmbOrderReply{
|
bizReply = &v1.CmbOrderReply{
|
||||||
RespCode: vo.CmbResponseStatusFail.GetValue(),
|
RespCode: vo.CmbResponseStatusFail.GetValue(),
|
||||||
RespMsg: err.Error(),
|
RespMsg: err.Error(),
|
||||||
|
|
@ -106,28 +110,27 @@ func (s *VoucherService) CmbQuery(ctx http.Context) error {
|
||||||
var (
|
var (
|
||||||
reply *v1.CmbReply
|
reply *v1.CmbReply
|
||||||
|
|
||||||
bizReply *v1.CmbQueryReply
|
req = &bo.CmbResponseBo{}
|
||||||
)
|
)
|
||||||
|
|
||||||
bizReply, err := s.cmbQuery(ctx)
|
bizReply, err := s.cmbQuery(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
se := errors.FromError(err)
|
|
||||||
|
|
||||||
bizReply = &v1.CmbQueryReply{
|
req.RespCode = vo.CmbResponseStatusFail.GetValue()
|
||||||
ThirdErrCode: se.Reason,
|
req.RespMsg = err.Error()
|
||||||
}
|
req.BizContent = ""
|
||||||
|
|
||||||
log.Errorf("CmbQuery error: %v", err)
|
log.Errorf("CmbQuery error: %v", err)
|
||||||
|
} else {
|
||||||
|
|
||||||
|
replyBizContentBytes, _ := json.Marshal(bizReply)
|
||||||
|
|
||||||
|
req.RespCode = vo.CmbResponseStatusSuccess.GetValue()
|
||||||
|
req.RespMsg = "成功"
|
||||||
|
req.BizContent = string(replyBizContentBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
replyBizContent, _ := json.Marshal(bizReply)
|
reply, err = s.CmbMixRepo.GetResponse(ctx, req)
|
||||||
xx := &bo.CmbResponseBo{
|
|
||||||
RespCode: vo.CmbResponseStatusSuccess.GetValue(),
|
|
||||||
RespMsg: "成功",
|
|
||||||
BizContent: string(replyBizContent),
|
|
||||||
}
|
|
||||||
|
|
||||||
reply, err = s.CmbMixRepo.GetResponse(ctx, xx)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("cmbProductQuery GetResponse error: %v", err)
|
log.Errorf("cmbProductQuery GetResponse error: %v", err)
|
||||||
return ctx.JSON(400, err)
|
return ctx.JSON(400, err)
|
||||||
|
|
@ -146,7 +149,7 @@ func (s *VoucherService) cmbQuery(ctx http.Context) (*v1.CmbQueryReply, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Warnf("cmbQuery req: %v", string(bodyBytes))
|
log.Warnf("cmbQuery req: %s", string(bodyBytes))
|
||||||
|
|
||||||
var req *v1.CmbRequest
|
var req *v1.CmbRequest
|
||||||
if err = json.Unmarshal(bodyBytes, &req); err != nil {
|
if err = json.Unmarshal(bodyBytes, &req); err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue