2016-07-07 97 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