feat: update app errors
This commit is contained in:
@@ -37,7 +37,7 @@ func (s *order) ListUserOrders(ctx context.Context, status string) ([]user_dto.O
|
||||
|
||||
list, err := q.Order(tbl.CreatedAt.Desc()).Find()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDatabaseError
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
var data []user_dto.Order
|
||||
@@ -61,7 +61,7 @@ func (s *order) GetUserOrder(ctx context.Context, id string) (*user_dto.Order, e
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errorx.ErrRecordNotFound
|
||||
}
|
||||
return nil, errorx.ErrDatabaseError
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
dto := s.toUserOrderDTO(item)
|
||||
@@ -87,7 +87,7 @@ func (s *order) Create(ctx context.Context, form *transaction_dto.OrderCreateFor
|
||||
|
||||
price, err := models.ContentPriceQuery.WithContext(ctx).Where(models.ContentPriceQuery.ContentID.Eq(cid)).First()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrDataCorrupted.WithMsg("价格信息缺失")
|
||||
return nil, errorx.ErrDataCorrupted.WithCause(err).WithMsg("价格信息缺失")
|
||||
}
|
||||
|
||||
// 2. Create Order (Status: Created)
|
||||
@@ -96,16 +96,16 @@ func (s *order) Create(ctx context.Context, form *transaction_dto.OrderCreateFor
|
||||
UserID: uid,
|
||||
Type: consts.OrderTypeContentPurchase,
|
||||
Status: consts.OrderStatusCreated,
|
||||
Currency: price.Currency,
|
||||
Currency: consts.Currency(price.Currency), // price.Currency is consts.Currency in DB? Yes.
|
||||
AmountOriginal: price.PriceAmount,
|
||||
AmountDiscount: 0, // Calculate discount if needed
|
||||
AmountPaid: price.PriceAmount, // Expected to pay
|
||||
IdempotencyKey: uuid.NewString(), // Should be from client ideally
|
||||
AmountDiscount: 0, // Calculate discount if needed
|
||||
AmountPaid: price.PriceAmount, // Expected to pay
|
||||
IdempotencyKey: uuid.NewString(), // Should be from client ideally
|
||||
Snapshot: types.NewJSONType(fields.OrdersSnapshot{}), // Populate details
|
||||
}
|
||||
|
||||
if err := models.OrderQuery.WithContext(ctx).Create(order); err != nil {
|
||||
return nil, errorx.ErrDatabaseError
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
// 3. Create Order Item
|
||||
@@ -118,7 +118,7 @@ func (s *order) Create(ctx context.Context, form *transaction_dto.OrderCreateFor
|
||||
AmountPaid: order.AmountPaid,
|
||||
}
|
||||
if err := models.OrderItemQuery.WithContext(ctx).Create(item); err != nil {
|
||||
return nil, errorx.ErrDatabaseError
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
return &transaction_dto.OrderCreateResponse{
|
||||
@@ -159,6 +159,7 @@ func (s *order) payWithBalance(ctx context.Context, o *models.Order) (*transacti
|
||||
info, err := tx.User.WithContext(ctx).
|
||||
Where(tx.User.ID.Eq(o.UserID), tx.User.Balance.Gte(o.AmountPaid)).
|
||||
Update(tx.User.Balance, gorm.Expr("balance - ?", o.AmountPaid))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -196,7 +197,7 @@ func (s *order) payWithBalance(ctx context.Context, o *models.Order) (*transacti
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
ledger := &models.TenantLedger{
|
||||
TenantID: o.TenantID,
|
||||
UserID: t.UserID, // Owner
|
||||
@@ -217,8 +218,12 @@ func (s *order) payWithBalance(ctx context.Context, o *models.Order) (*transacti
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if _, ok := err.(*errorx.AppError); ok {
|
||||
return nil, err
|
||||
}
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
|
||||
return &transaction_dto.OrderPayResponse{
|
||||
@@ -238,4 +243,4 @@ func (s *order) toUserOrderDTO(o *models.Order) user_dto.Order {
|
||||
Amount: float64(o.AmountPaid) / 100.0,
|
||||
CreateTime: o.CreatedAt.Format(time.RFC3339),
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user