java - 错误管理

标签 java error-handling

我在休息架构中使用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中累积失败的ExceptionsprocessCreditCardPayment

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/

相关文章:

angular - Angular 中模块特定的错误处理

php - 意外> T_STRING,期望为T_OLD_FUNCTION或> T_FUNCTION或T_VAR或 '}'

java - 无法解析类型 org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl。它是从所需的 .class 文件间接引用的

java - 在没有向导的情况下对 Eclipse Java 项目进行 Mavening

c# - 使用 MSMQ 将失败的消息与 NServiceBus 中的异常匹配

http - 尝试用DB编写时应如何处理错误

java - Gradle 3.0.0 的 Android 插件 : Could not find com. google.http-client :google-http-client-parent:1. 24.1

java - 后台嵌入浏览器,用于爬取JAVA

java - 根据 JComboBox 值从 xml 文件获取下一个兄弟值到 java 并在控制台打印

windows - 诸如logcat之类的任何类型的软件都可以在Windows Xp os中查看错误日志