Browse Source

fix: 异常处理

lixuesong 1 năm trước cách đây
mục cha
commit
6e0f699c4a

+ 49 - 0
hnqz-common/hnqz-common-core/src/main/java/com/qunzhixinxi/hnqz/common/core/exception/BizException.java

@@ -0,0 +1,49 @@
+/*
+ *
+ *      Copyright (c) 2018-2025, hnqz All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ *  this list of conditions and the following disclaimer.
+ *  Redistributions in binary form must reproduce the above copyright
+ *  notice, this list of conditions and the following disclaimer in the
+ *  documentation and/or other materials provided with the distribution.
+ *  Neither the name of the pig4cloud.com developer nor the names of its
+ *  contributors may be used to endorse or promote products derived from
+ *  this software without specific prior written permission.
+ *  Author: hnqz
+ *
+ */
+
+package com.qunzhixinxi.hnqz.common.core.exception;
+
+import lombok.NoArgsConstructor;
+
+/**
+ * @author hnqz
+ * @date 😴2018年06月22日16:21:57
+ */
+@NoArgsConstructor
+public class BizException extends RuntimeException {
+
+	private static final long serialVersionUID = 1L;
+
+	public BizException(String message) {
+		super(message);
+	}
+
+	public BizException(Throwable cause) {
+		super(cause);
+	}
+
+	public BizException(String message, Throwable cause) {
+		super(message, cause);
+	}
+
+	public BizException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
+		super(message, cause, enableSuppression, writableStackTrace);
+	}
+
+}

+ 14 - 0
hnqz-common/hnqz-common-security/src/main/java/com/qunzhixinxi/hnqz/common/security/component/GlobalExceptionHandlerResolver.java

@@ -17,6 +17,7 @@
 
 package com.qunzhixinxi.hnqz.common.security.component;
 
+import com.qunzhixinxi.hnqz.common.core.exception.BizException;
 import com.qunzhixinxi.hnqz.common.core.util.R;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.HttpStatus;
@@ -93,4 +94,17 @@ public class GlobalExceptionHandlerResolver {
 		return R.failed(fieldErrors.get(0).getDefaultMessage());
 	}
 
+	/**
+	 * 业务异常处理
+	 *
+	 * @param exception
+	 * @return R
+	 */
+	@ExceptionHandler({ BizException.class })
+	@ResponseStatus(HttpStatus.BAD_REQUEST)
+	public R bizExceptionHandler(BizException exception) {
+		log.warn("业务处理异常,ex = {}", exception.getMessage());
+		return R.failed(exception.getMessage());
+	}
+
 }

+ 5 - 3
hnqz-upms/hnqz-upms-biz/src/main/java/com/qunzhixinxi/hnqz/admin/controller/WmPayOffController.java

@@ -35,6 +35,7 @@ import com.qunzhixinxi.hnqz.admin.service.WmScorePackageService;
 import com.qunzhixinxi.hnqz.admin.service.WmScorePackageSettleNoteService;
 import com.qunzhixinxi.hnqz.admin.util.AmountToCNUtil;
 import com.qunzhixinxi.hnqz.admin.util.OsEnvUtils;
+import com.qunzhixinxi.hnqz.common.core.exception.BizException;
 import com.qunzhixinxi.hnqz.common.core.util.R;
 import com.qunzhixinxi.hnqz.common.ding.enums.DingEnum;
 import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
@@ -49,6 +50,7 @@ import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.data.redis.core.script.DefaultRedisScript;
+import org.springframework.security.access.AccessDeniedException;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
@@ -408,13 +410,13 @@ public class WmPayOffController {
 	 */
 	@SysLog("导出结算单")
 	@GetMapping("/exportSettlementProof")
-	public void exportSettlementProof(@RequestParam("packageIds") List<String> packageIds, HttpServletResponse response) {
+	public void exportSettlementProof(@RequestParam("packageIds") List<String> packageIds, HttpServletResponse response) throws IllegalAccessException {
 		log.info("导出结算单参数packageIds={}", packageIds);
 
 		Integer deptId = SecurityUtils.getUser().getDeptId();
 		SysDept dept = sysDeptService.getById(deptId);
 		if (!ArrayUtil.contains(dept.getDeptPermissions(), DeptPermissionEnum.SETTLEMENT_PROOF.val())) {
-			throw new RuntimeException("没有权限");
+			throw new AccessDeniedException("没有权限");
 		}
 
 		// 查询积分包
@@ -424,7 +426,7 @@ public class WmPayOffController {
 				wmScorePackageSettleNoteService.list(Wrappers.<WmScorePackageSettleNote>lambdaQuery()
 						.in(WmScorePackageSettleNote::getPackageId, packageIds));
 		if (CollUtil.isEmpty(settleNoteList)) {
-			throw new RuntimeException("结算信息为空");
+			throw new BizException("结算信息为空");
 		}
 
 		Map<String, List<WmScorePackageSettleNote>> settleNoteMap = settleNoteList.stream()