额外的异常

This commit is contained in:
2025-11-12 13:30:57 +08:00
parent 8634f1a5a6
commit 89762d0589
3 changed files with 9 additions and 38 deletions

View File

@@ -144,39 +144,10 @@ public class CzgControllerAdvice {
@ExceptionHandler(value = Exception.class) @ExceptionHandler(value = Exception.class)
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public CzgResult<Object> errorHandler(Exception ex) { public CzgResult<Object> errorHandler(Exception ex) {
Throwable rootCause = ex; // setErrorLog(ex);
log.info("rootCause2:{}", rootCause.getClass()); // 3. 处理未捕获的异常(系统异常,隐藏敏感信息)
// 处理自定义异常 log.error("系统未处理异常", ex); // 替换原有 setErrorLog使用日志框架
if (rootCause instanceof CzgException czgException) { return CzgResult.failure(CzgRespCode.SYSTEM_ERROR.getCode(), "系统繁忙,请稍后再试");
return CzgResult.failure(czgException.getCode(), czgException.getMessage());
}
if (rootCause instanceof OrderValidateException orderValidateException) {
return CzgResult.failure(orderValidateException.getCode(), orderValidateException.getMessage());
}
if (rootCause instanceof OrderCancelException) {
return CzgResult.failure(701, "订单已过期,请重新下单");
}
if (rootCause instanceof PaySuccessException) {
return CzgResult.success("支付成功");
}
while (rootCause.getCause() != null) {
log.info("rootCause1:{}", rootCause.getClass());
rootCause = rootCause.getCause();
if (rootCause instanceof CzgException czgException) {
return CzgResult.failure(czgException.getCode(), czgException.getMessage());
}
if (rootCause instanceof OrderValidateException orderValidateException) {
return CzgResult.failure(orderValidateException.getCode(), orderValidateException.getMessage());
}
if (rootCause instanceof PaySuccessException) {
return CzgResult.failure(701, "订单已过期,请重新下单");
}
if (rootCause instanceof OrderCancelException) {
return CzgResult.success("支付成功");
}
}
setErrorLog(ex);
return CzgResult.failure(CzgRespCode.SYSTEM_ERROR.getCode(), ex.getMessage());
} }
private void setErrorLog(Exception ex) { private void setErrorLog(Exception ex) {

View File

@@ -58,7 +58,7 @@ public interface OrderInfoService extends IService<OrderInfo> {
Boolean removeOrderDetail(Long shopId, Long orderId, Long detailId); Boolean removeOrderDetail(Long shopId, Long orderId, Long detailId);
Boolean cancelledOrder(Long shopId, Long orderId); Boolean cancelledOrder(Long shopId, Long orderId) throws OrderCancelException ;
Boolean cancelledPlaceOrder(Long shopId, Long orderId, Integer placeNum); Boolean cancelledPlaceOrder(Long shopId, Long orderId, Integer placeNum);

View File

@@ -264,7 +264,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
@Override @Override
@Transactional @Transactional
public OrderInfo createOrder(OrderInfoAddDTO param) { public OrderInfo createOrder(OrderInfoAddDTO param) throws ValidateException{
ShopInfo shopInfo = shopInfoService.getById(param.getShopId()); ShopInfo shopInfo = shopInfoService.getById(param.getShopId());
AssertUtil.isNull(shopInfo, "生成订单失败,店铺信息不存在"); AssertUtil.isNull(shopInfo, "生成订单失败,店铺信息不存在");
if (!shopInfo.getEatModel().contains(param.getDineMode())) { if (!shopInfo.getEatModel().contains(param.getDineMode())) {
@@ -1596,13 +1596,13 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Boolean cancelledOrder(Long shopId, Long orderId) { public Boolean cancelledOrder(Long shopId, Long orderId) throws OrderCancelException {
OrderInfo orderInfo = getById(orderId); OrderInfo orderInfo = getById(orderId);
if (orderInfo == null) { if (orderInfo == null) {
throw new ApiNotPrintException("订单不存在"); throw new OrderCancelException("订单不存在");
} }
if (!OrderStatusEnums.UNPAID.getCode().equals(orderInfo.getStatus())) { if (!OrderStatusEnums.UNPAID.getCode().equals(orderInfo.getStatus())) {
throw new ApiNotPrintException("订单不可取消"); throw new OrderCancelException("订单不可取消");
} }
updateChain() updateChain()
.set(OrderInfo::getStatus, OrderStatusEnums.CANCELLED.getCode()) .set(OrderInfo::getStatus, OrderStatusEnums.CANCELLED.getCode())