2016-07-07 75 views
-1

我在休息架构中使用弹簧引导。 我是一名具有错误管理的新手。错误管理

我的代码有这样的结构

@Transactional 
@Override 
public void processPayment() throws CreditCardPaymentException{ 
    List<Payment> payments = paymentRepository.findByDateLessThanEqualAndPaymentModeAndStatus(LocalDate.now(), PaymentModeEnum.CREDITCARD, StatusEnum.STANDBY); 
    processCreditCardPayment(payments); 
} 

private void processCreditCardPayment(List<Payment> payments) throws ProcessPaymentException{ 
    PaymentGatewayConfig paymentGateway = new PaymentGatewayConfig(); 
    String crypt_type = "7"; 
    for (Payment payment : payments) { 
     chargeMemberCreditCard(payment, crypt_type, paymentGateway); 
    } 
} 

private ResolverReceipt chargeMemberCreditCard(Payment payment, String crypt_type, PaymentGatewayConfig paymentGateway) throws ProcessPaymentException { 

    try { 
     if (resreceipt != null) { 

      //information about customer we have sent are returned 
      ResolveData resdata = resreceipt.getResolveData(); 
      //todo check auth code 
      if (Boolean.valueOf(resreceipt.getComplete()) && !Boolean.valueOf(resreceipt.getTimedOut())) { 
       //if (resreceipt != null && resreceipt.getResponseCode() != null && Integer.getInteger(resreceipt.getResponseCode()) < 50) { 
       payment.setStatus(StatusEnum.COMPLETE); 
      } else { 
       payment.setStatus(StatusEnum.FAIL); 
      } 
     } 

    } catch (Exception e) { 
     throw new ProcessPaymentException(); 
     log.error("chargeMemberCreditCard - payment: " + payment.getPaymentId(), e); 
    } 

} 

如果有错误付款,我想传递给下一个。 如果最后有很多错误,我只想知道发生了什么错误。

不知道是否该走的路,如果这段代码会这样做。

+0

try/catch语句与失败的列表失败PaymentsExceptions? –

回答

0

积攒你的processCreditCardPayment

private void processCreditCardPayment(List<Payment> payments) throws ProcessPaymentException{ 
PaymentGatewayConfig paymentGateway = new PaymentGatewayConfig(); 
String crypt_type = "7"; 
List<Payment> failedPayments = new ArrayList<Payments>(); 
for (Payment payment : payments) { 
    try { 
     chargeMemberCreditCard(payment, crypt_type, paymentGateway); 
    } catch (ProcessPaymentException ppe) { 
     filedPayments.add(payment); 
     // or you can accumulate excetions instead of Payments 
    } 
} 
// create some higher level exception with failed Payent collection and throw it, or log it. 
+0

如果我明白,在ProcessPaymentException中,我必须有一个付款清单? –

+0

不,在每个循环中你已经有付款。 您只需在每次可以在'chargeMemberCreditCard'中引发的迭代中捕获异常。当您发现异常时,您将添加导致filedPayments列表中发生异常的付款。 ProcessPaymentException根本不包含任何特殊数据。 – heyDude