feat 异常体系完善

This commit is contained in:
DaxPay
2024-06-19 21:37:56 +08:00
parent bd11adee36
commit 777d691d71
40 changed files with 200 additions and 95 deletions

View File

@@ -1,5 +1,7 @@
package cn.daxpay.single.service.common.context;
import cn.bootx.platform.common.core.exception.BizException;
import cn.daxpay.single.core.code.DaxPayErrorCode;
import lombok.Data;
import lombok.experimental.Accessors;
@@ -13,8 +15,30 @@ import lombok.experimental.Accessors;
public class ErrorInfoLocal {
/** 错误码 */
private String errorCode;
private int errorCode;
/** 错误内容 */
private String errorMsg;
/**
* 自动根据传入的异常对象对象进行处理
*/
public void setException(Exception e) {
// 如果业务异常, 获取错误码和错误信息
if (e instanceof BizException) {
BizException be = (BizException) e;
this.errorCode = be.getCode();
this.errorMsg = be.getMessage();
}
// 如果是空指针, 专门记录
else if (e instanceof NullPointerException) {
this.errorCode = DaxPayErrorCode.UNCLASSIFIED_ERROR;
this.errorMsg = "空指针异常";
}
// 如果是其他异常, 归类到为止异常中
else {
this.errorCode = DaxPayErrorCode.SYSTEM_UNKNOWN_ERROR;
this.errorMsg = "未归类异常,请联系管理人员进行排查";
}
}
}

View File

@@ -1,5 +1,6 @@
package cn.daxpay.single.service.core.channel.alipay.service;
import cn.daxpay.single.core.code.DaxPayErrorCode;
import cn.daxpay.single.core.code.RefundStatusEnum;
import cn.daxpay.single.core.exception.OperationFailException;
import cn.daxpay.single.core.util.PayUtil;
@@ -53,10 +54,10 @@ public class AliPayRefundService {
try {
AlipayTradeRefundResponse response = alipayClient.execute(request);
if (!Objects.equals(AliPayCode.SUCCESS, response.getCode())) {
errorInfo.setErrorMsg(response.getSubMsg());
errorInfo.setErrorCode(response.getCode());
OperationFailException operationFailException = new OperationFailException(response.getSubMsg());
errorInfo.setException(operationFailException);
log.error("网关返回退款失败: {}", response.getSubMsg());
throw new OperationFailException(response.getSubMsg());
throw operationFailException;
}
// 默认为退款中状态
refundInfo.setStatus(RefundStatusEnum.PROGRESS)
@@ -71,8 +72,8 @@ public class AliPayRefundService {
catch (AlipayApiException e) {
log.error("订单退款失败:", e);
errorInfo.setErrorMsg(e.getErrMsg());
errorInfo.setErrorCode(e.getErrCode());
throw new OperationFailException("订单退款失败");
errorInfo.setErrorCode(DaxPayErrorCode.OPERATION_FAIL);
throw new OperationFailException(e.getErrMsg());
}
}
}

View File

@@ -20,7 +20,6 @@ import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
import java.util.Map;
import java.util.Optional;
import static cn.daxpay.single.service.code.WeChatPayCode.*;
@@ -58,9 +57,9 @@ public class WeChatPayRefundService {
// 获取证书文件
if (StrUtil.isBlank(weChatPayConfig.getP12())){
String errorMsg = "微信p.12证书未配置,无法进行退款";
errorInfo.setErrorMsg(errorMsg);
errorInfo.setErrorCode(RefundStatusEnum.FAIL.getCode());
throw new ConfigErrorException(errorMsg);
ConfigErrorException configErrorException = new ConfigErrorException(errorMsg);
errorInfo.setException(configErrorException);
throw configErrorException;
}
byte[] fileBytes = Base64.decode(weChatPayConfig.getP12());
ByteArrayInputStream inputStream = new ByteArrayInputStream(fileBytes);
@@ -85,10 +84,10 @@ public class WeChatPayRefundService {
errorMsg = result.get(RETURN_MSG);
}
log.error("订单退款失败 {}", errorMsg);
TradeFaileException tradeFaileException = new TradeFaileException(errorMsg);
ErrorInfoLocal errorInfo = PaymentContextLocal.get().getErrorInfo();
errorInfo.setErrorMsg(errorMsg);
errorInfo.setErrorCode(Optional.ofNullable(resultCode).orElse(returnCode));
throw new TradeFaileException(errorMsg);
errorInfo.setException(tradeFaileException);
throw tradeFaileException;
}
}

View File

@@ -1,5 +1,6 @@
package cn.daxpay.single.service.core.order.transfer.service;
import cn.daxpay.single.core.param.payment.transfer.TransferParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -17,7 +18,7 @@ public class TransferOrderService {
/**
* 手动转账
*/
public void transfer(){
public void transfer(TransferParam param){
}

View File

@@ -120,7 +120,7 @@ public class RefundAssistService {
public void updateOrderByError(RefundOrder refundOrder){
RefundLocal refundInfo = PaymentContextLocal.get().getRefundInfo();
ErrorInfoLocal errorInfo = PaymentContextLocal.get().getErrorInfo();
refundOrder.setErrorCode(errorInfo.getErrorCode());
refundOrder.setErrorCode(String.valueOf(errorInfo.getErrorCode()));
refundOrder.setErrorMsg(errorInfo.getErrorMsg());
refundOrder.setStatus(refundInfo.getStatus().getCode());
refundOrderManager.updateById(refundOrder);

View File

@@ -56,7 +56,7 @@ public class TransferAssistService {
ErrorInfoLocal errorInfo = PaymentContextLocal.get().getErrorInfo();
order.setStatus(TransferStatusEnum.FAIL.getCode())
.setErrorMsg(errorInfo.getErrorMsg())
.setErrorCode(errorInfo.getErrorCode());
.setErrorCode(String.valueOf(errorInfo.getErrorCode()));
transferOrderManager.updateById(order);
}

View File

@@ -33,7 +33,6 @@ public class TransferService {
* 转账
*/
public TransferResult transfer(TransferParam transferParam){
// 获取策略
AbsTransferStrategy transferStrategy = PayStrategyFactory.create(transferParam.getChannel(),AbsTransferStrategy.class);
// 检查转账参数
@@ -50,6 +49,7 @@ public class TransferService {
log.error("转账出现错误", e);
// 记录处理失败状态
PaymentContextLocal.get().getRefundInfo().setStatus(RefundStatusEnum.FAIL);
PaymentContextLocal.get().getErrorInfo().setException(e);
// 更新退款失败的记录
transferAssistService.updateOrderByError(order);
return transferAssistService.buildResult(order);