Java Rethrow原始异常when catch子句有另一个try catch,它在反应性管道中不抛出任何等价项
我有一个传统的、命令式的Java Rethrow原始异常when catch子句有另一个try catch,它在反应性管道中不抛出任何等价项,java,reactive-programming,rethrow,Java,Reactive Programming,Rethrow,我有一个传统的、命令式的try-catch编码风格的方法: void confirmCart(Checkout Order) { try { cartService.confirm(order.getCartId(), order.getCartHash()); } catch (Exception ex1) { log.error("Confirm error: ", ex1); try { payment
try-catch
编码风格的方法:
void confirmCart(Checkout Order) {
try {
cartService.confirm(order.getCartId(), order.getCartHash());
} catch (Exception ex1) {
log.error("Confirm error: ", ex1);
try {
paymentService.cancel(order.getPaymentTransaction().getGatewayReference());
throw ServiceException.wrap(ex1, CheckoutErrorCode.FAILED_CONFIRMING_CART);
} catch (Exception ex2) {
throw ServiceException.wrap(ex2, CheckoutErrorCode.FAILED_CANCELLING_PAYMENT);
}
}
}
要求是:
- 当
cartService.confirm()起作用时,不返回任何内容
- 失败时,输入catch part,然后尝试
paymentService.cancel()
- 如果2失败,抛出
ex2
- 如果2成功,抛出
(因为ex1
还是失败了)confirm()
- 根据这里抛出的异常,控制器将执行不同的操作
Mono<Void> confirmCart(CheckoutOrder order) {
return cartService.confirm(order.getCartId(), order.getCartHash())
.onErrorMap(throwable -> {
log.error("Failed confirming cart! cartId: '{}', cartHash: '{}'", order.getCartId(), order.getCartHash(), throwable);
return ServiceException.wrap(throwable, CheckoutErrorCode.FAILED_CONFIRMING_CART);
})
.onErrorResume(throwable -> {
log.trace("Cancelling payment with order id {}", order.getId().toString());
// when confirmation fails, 1. cancel payment and 2. throw original exception to notify controller -> exception handler
return paymentService.cancel(order.getPaymentTransaction().getGatewayReference())
.map(paymentCancellationResponse -> {
log.trace("payment cancellation response: {}, order id: {}", paymentCancellationResponse, order.getId().toString());
return paymentCancellationResponse;
}).then()
.onErrorMap(e -> {
log.error("payment payment cancellation failed. ", e);
return ServiceException.wrap(e, CheckoutErrorCode.FAILED_CANCELLING_PAYMENT);
});
});
}
在测试中,当cartService.confirm()失败时,我没有抛出异常
return cartService.confirm(order.getCartId(), order.getCartHash())
.onErrorResume(throwable -> {
log.error("Failed confirming cart! cartId: '{}', cartHash: '{}'", order.getCartId(), order.getCartHash(), throwable);
log.trace("Cancelling payment with order id {}", order.getId().toString());
// when confirmation fails, 1. cancel payment and 2. redirect user to checkout page
return paymentService.cancel(order.getPaymentTransaction().getGatewayReference())
.map(paymentCancellationResponse -> {
log.trace("payment cancellation response: {}, order id: {}", paymentCancellationResponse, order.getId().toString());
return paymentCancellationResponse;
}).thenReturn(
ServiceException.wrap(throwable, CheckoutErrorCode.FAILED_CONFIRMING_CART)
)
.onErrorMap(e -> {
log.error("payment payment cancellation failed. ", e);
return ServiceException.wrap(e, CheckoutErrorCode.FAILED_CANCELLING_PAYMENT);
}).then();
});