自定义错误

This commit is contained in:
李子铭 2025-03-14 21:11:00 +08:00
parent b29c603199
commit 2e648c09c3
8 changed files with 20 additions and 11 deletions

View File

@ -10,6 +10,9 @@ enum Err {
// panic错误 // panic错误
SYSTEM_PANIC = 0 [(errors.code) = 599]; SYSTEM_PANIC = 0 [(errors.code) = 599];
// DB数据未找到
DB_NOT_FOUND = 2 [(errors.code) = 404];
} }
enum NotifyConsumeErr{ enum NotifyConsumeErr{

View File

@ -2,9 +2,7 @@ package biz
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"gorm.io/gorm"
"time" "time"
err2 "voucher/api/err" err2 "voucher/api/err"
v1 "voucher/api/v1" v1 "voucher/api/v1"
@ -21,7 +19,7 @@ func (v *VoucherBiz) CmbOrder(ctx context.Context, req *bo.OrderCreateReqBo) (vo
order, err := v.OrderRepo.GetByOutBizNo(ctx, vo.OrderTypeCmb, req.OutBizNo) order, err := v.OrderRepo.GetByOutBizNo(ctx, vo.OrderTypeCmb, req.OutBizNo)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { if err != nil && !err2.IsDbNotFound(err) {
return err return err
} }

View File

@ -2,9 +2,17 @@ package repoimpl
import ( import (
"time" "time"
err2 "voucher/api/err"
"voucher/internal/pkg/mapstructure" "voucher/internal/pkg/mapstructure"
) )
var (
ErrOrderRecordNotFound = err2.ErrorDbNotFound("订单不存在")
ErrProductRecordNotFound = err2.ErrorDbNotFound("商品不存在")
ErrOrderNotifyRecordNotFound = err2.ErrorDbNotFound("订单通知数据不存在")
ErrWechatNotifyRegisterTagRecordNotFound = err2.ErrorDbNotFound("微信注册tag数据不存在")
)
type Base[D any, B any] struct{} type Base[D any, B any] struct{}
func (*Base[D, B]) ToBo(d *D) *B { func (*Base[D, B]) ToBo(d *D) *B {

View File

@ -91,7 +91,7 @@ func (p *OrderRepoImpl) GetByOutBizNo(ctx context.Context, t vo.OrderType, outBi
} }
if tx.RowsAffected == 0 { if tx.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound return nil, ErrOrderRecordNotFound
} }
return p.ToBo(info), nil return p.ToBo(info), nil
@ -107,7 +107,7 @@ func (p *OrderRepoImpl) GetByID(ctx context.Context, id uint64) (*bo.OrderBo, er
} }
if tx.RowsAffected == 0 { if tx.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound return nil, ErrOrderRecordNotFound
} }
return p.ToBo(info), nil return p.ToBo(info), nil
@ -123,7 +123,7 @@ func (p *OrderRepoImpl) GetByOrderNo(ctx context.Context, orderNo string) (*bo.O
} }
if tx.RowsAffected == 0 { if tx.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound return nil, ErrOrderRecordNotFound
} }
return p.ToBo(info), nil return p.ToBo(info), nil
@ -139,7 +139,7 @@ func (p *OrderRepoImpl) GetByMBV(ctx context.Context, merchantNo, batchNo, vouch
} }
if tx.RowsAffected == 0 { if tx.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound return nil, ErrOrderRecordNotFound
} }
return p.ToBo(info), nil return p.ToBo(info), nil

View File

@ -37,7 +37,7 @@ func (p *OrderNotifyRepoImpl) GetByID(ctx context.Context, id uint64) (*bo.Order
} }
if tx.RowsAffected == 0 { if tx.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound return nil, ErrOrderNotifyRecordNotFound
} }
return p.ToBo(info), nil return p.ToBo(info), nil

View File

@ -34,7 +34,7 @@ func (r *ProductRepoImpl) GetByPNO(ctx context.Context, PNO string) (*bo.Product
} }
if tx.RowsAffected == 0 { if tx.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound return nil, ErrProductRecordNotFound
} }
return r.ToBo(&item), nil return r.ToBo(&item), nil

View File

@ -37,7 +37,7 @@ func (p *WechatNotifyRegisterTagRepoImpl) GetByStockIdAndMchId(ctx context.Conte
} }
if tx.RowsAffected == 0 { if tx.RowsAffected == 0 {
return nil, gorm.ErrRecordNotFound return nil, ErrWechatNotifyRegisterTagRecordNotFound
} }
return p.ToBo(info), nil return p.ToBo(info), nil

View File

@ -31,7 +31,7 @@ func (s *VoucherService) CmbOrder(ctx http.Context) error {
bizReply = &v1.CmbOrderReply{ bizReply = &v1.CmbOrderReply{
RespCode: vo.CmbResponseStatusFail.GetValue(), RespCode: vo.CmbResponseStatusFail.GetValue(),
RespMsg: err.Error(), RespMsg: se.Message,
CodeNo: "", CodeNo: "",
ThirdErrCode: se.Reason, ThirdErrCode: se.Reason,
} }