Browse Source

feat: only biz exception

shc 4 months ago
parent
commit
4ab3b75779

+ 32 - 0
hnqz-common/hnqz-common-core/src/main/java/com/qunzhixinxi/hnqz/common/core/exception/OnlyBizException.java

@@ -0,0 +1,32 @@
+
+
+package com.qunzhixinxi.hnqz.common.core.exception;
+
+import lombok.NoArgsConstructor;
+
+/**
+ * @author hnqz
+ * @date 😴2018年06月22日16:21:57
+ */
+@NoArgsConstructor
+public class OnlyBizException extends RuntimeException {
+
+	private static final long serialVersionUID = 1L;
+
+	public OnlyBizException(String message) {
+		super(message);
+	}
+
+	public OnlyBizException(Throwable cause) {
+		super(cause);
+	}
+
+	public OnlyBizException(String message, Throwable cause) {
+		super(message, cause);
+	}
+
+	public OnlyBizException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
+		super(message, cause, enableSuppression, writableStackTrace);
+	}
+
+}

+ 80 - 63
hnqz-common/hnqz-common-security/src/main/java/com/qunzhixinxi/hnqz/common/security/component/GlobalExceptionHandlerResolver.java

@@ -1,7 +1,7 @@
-
 package com.qunzhixinxi.hnqz.common.security.component;
 
 import com.qunzhixinxi.hnqz.common.core.exception.BizException;
+import com.qunzhixinxi.hnqz.common.core.exception.OnlyBizException;
 import com.qunzhixinxi.hnqz.common.core.util.R;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.HttpStatus;
@@ -24,71 +24,88 @@ import java.util.List;
 @RestControllerAdvice
 public class GlobalExceptionHandlerResolver {
 
-	/**
-	 * 全局异常.
-	 * @param e the e
-	 * @return R
-	 */
-	@ExceptionHandler(Exception.class)
-	@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
-	public R handleGlobalException(Exception e) {
-		log.error("全局异常信息 ex={}", e.getMessage(), e);
-		log.error("全局异常信息", e);
-		log.error("全局异常信息 >>> " + e.getMessage());
-		return R.failed(e.getLocalizedMessage());
-	}
+  /**
+   * 全局异常.
+   *
+   * @param e the e
+   * @return R
+   */
+  @ExceptionHandler(Exception.class)
+  @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
+  public R handleGlobalException(Exception e) {
+    log.error("全局异常信息 ex={}", e.getMessage(), e);
+    log.error("全局异常信息", e);
+    log.error("全局异常信息 >>> " + e.getMessage());
+    return R.failed(e.getLocalizedMessage());
+  }
 
-	/**
-	 * AccessDeniedException
-	 * @param e the e
-	 * @return R
-	 */
-	@ExceptionHandler(AccessDeniedException.class)
-	@ResponseStatus(HttpStatus.FORBIDDEN)
-	public R handleAccessDeniedException(AccessDeniedException e) {
-		String msg = SpringSecurityMessageSource.getAccessor().getMessage("AbstractAccessDecisionManager.accessDenied",
-				e.getMessage());
-		log.error("拒绝授权异常信息 ex={}", msg, e);
-		return R.failed(e.getLocalizedMessage());
-	}
+  /**
+   * AccessDeniedException
+   *
+   * @param e the e
+   * @return R
+   */
+  @ExceptionHandler(AccessDeniedException.class)
+  @ResponseStatus(HttpStatus.FORBIDDEN)
+  public R handleAccessDeniedException(AccessDeniedException e) {
+    String msg =
+        SpringSecurityMessageSource.getAccessor()
+            .getMessage("AbstractAccessDecisionManager.accessDenied", e.getMessage());
+    log.error("拒绝授权异常信息 ex={}", msg, e);
+    return R.failed(e.getLocalizedMessage());
+  }
 
-	/**
-	 * validation Exception
-	 * @param exception
-	 * @return R
-	 */
-	@ExceptionHandler({ MethodArgumentNotValidException.class })
-	@ResponseStatus(HttpStatus.BAD_REQUEST)
-	public R handleBodyValidException(MethodArgumentNotValidException exception) {
-		List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
-		log.warn("参数绑定异常,ex = {}", fieldErrors.get(0).getDefaultMessage());
-		return R.failed(fieldErrors.get(0).getDefaultMessage());
-	}
+  /**
+   * validation Exception
+   *
+   * @param exception
+   * @return R
+   */
+  @ExceptionHandler({MethodArgumentNotValidException.class})
+  @ResponseStatus(HttpStatus.BAD_REQUEST)
+  public R handleBodyValidException(MethodArgumentNotValidException exception) {
+    List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
+    log.warn("参数绑定异常,ex = {}", fieldErrors.get(0).getDefaultMessage());
+    return R.failed(fieldErrors.get(0).getDefaultMessage());
+  }
 
-	/**
-	 * validation Exception (以form-data形式传参)
-	 * @param exception
-	 * @return R
-	 */
-	@ExceptionHandler({ BindException.class })
-	@ResponseStatus(HttpStatus.BAD_REQUEST)
-	public R bindExceptionHandler(BindException exception) {
-		List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
-		log.warn("参数绑定异常,ex = {}", fieldErrors.get(0).getDefaultMessage());
-		return R.failed(fieldErrors.get(0).getDefaultMessage());
-	}
+  /**
+   * validation Exception (以form-data形式传参)
+   *
+   * @param exception
+   * @return R
+   */
+  @ExceptionHandler({BindException.class})
+  @ResponseStatus(HttpStatus.BAD_REQUEST)
+  public R bindExceptionHandler(BindException exception) {
+    List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
+    log.warn("参数绑定异常,ex = {}", fieldErrors.get(0).getDefaultMessage());
+    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());
-	}
+  /**
+   * 业务异常处理
+   *
+   * @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());
+  }
 
+  /**
+   * 业务异常处理(http状态正常)
+   *
+   * @param exception 异常
+   * @return R
+   */
+  @ExceptionHandler({OnlyBizException.class})
+  @ResponseStatus(HttpStatus.OK)
+  public R onlyBizExceptionHandler(OnlyBizException exception) {
+    log.warn("业务处理异常,ex = {}", exception.getMessage());
+    return R.failed(exception.getMessage());
+  }
 }

+ 5 - 4
hnqz-upms/hnqz-upms-biz/src/main/java/com/qunzhixinxi/hnqz/admin/controller/task/WmTaskControllerV2.java

@@ -15,6 +15,7 @@ import com.qunzhixinxi.hnqz.admin.manager.TaskManager;
 import com.qunzhixinxi.hnqz.admin.util.RedisUtils;
 import com.qunzhixinxi.hnqz.admin.util.RedisUtils.Token;
 import com.qunzhixinxi.hnqz.common.core.exception.BizException;
+import com.qunzhixinxi.hnqz.common.core.exception.OnlyBizException;
 import com.qunzhixinxi.hnqz.common.core.util.R;
 import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
 import com.qunzhixinxi.hnqz.common.security.service.HnqzUser;
@@ -145,12 +146,12 @@ public class WmTaskControllerV2 {
     // 删除幂等token
     boolean valid = redisUtils.validToken(checkReq.getToken(), checkReq.getTaskId());
     if (!valid) {
-      throw new BizException("审核令牌无效,请刷新数据后重试");
+      throw new OnlyBizException("审核令牌无效,请刷新数据后重试");
     }
 
     // 校验参数
     if (!checkReq.getCheckResult() && StrUtil.isBlank(checkReq.getCheckMessage())) {
-      throw new BizException("拒绝时,【审核意见】必填");
+      throw new OnlyBizException("拒绝时,【审核意见】必填");
     }
 
     // 添加执行锁
@@ -165,7 +166,7 @@ public class WmTaskControllerV2 {
         redisTemplate.opsForValue().setIfAbsent(lockKey, lockInfo, 60 * 60 * 24, TimeUnit.SECONDS);
 
     if (Boolean.FALSE.equals(locked)) {
-      throw new BizException(
+      throw new OnlyBizException(
           "当前任务(ID:"
               + checkReq.getTaskId()
               + "),存在在途操作,"
@@ -179,7 +180,7 @@ public class WmTaskControllerV2 {
     } catch (Exception e) {
       log.error(e.getMessage(), e);
       if (e instanceof BizException) {
-        throw (BizException) e;
+        throw new OnlyBizException(e.getMessage());
       } else {
         redisTemplate.delete(lockKey);
       }