我在休息架构中使用spring boot。
我是一个具有错误管理的新手。
我的代码具有这种结构
@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);
}
}
如果付款有误,我想转到下一笔。
如果最后有很多错误,我只想知道发生了什么错误。
不确定是否要这样做,并且此代码是否可以做到这一点。
最佳答案
在Payments
中累积失败的Exceptions
或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.
关于java - 错误管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38250197/