|
|
@@ -15,7 +15,6 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
import java.nio.file.AccessDeniedException;
|
|
|
import java.util.List;
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* <p>
|
|
|
* 全局异常处理器
|
|
|
@@ -29,112 +28,109 @@ import java.util.List;
|
|
|
@Order(10000)
|
|
|
public class GlobalBizExceptionHandler {
|
|
|
|
|
|
- /**
|
|
|
- * 全局异常.
|
|
|
- * @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);
|
|
|
-
|
|
|
- return R.failed(e.getLocalizedMessage());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 运行时异常.
|
|
|
- * @param e the e
|
|
|
- * @return R
|
|
|
- */
|
|
|
- @ExceptionHandler(RuntimeException.class)
|
|
|
- @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
- public R handleGlobalRuntimeException(RuntimeException e) {
|
|
|
- log.error("运行时异常 ex={}", e.getMessage(), e);
|
|
|
-
|
|
|
- return R.failed(e.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理业务校验过程中碰到的非法参数异常 该异常基本由{@link Assert}抛出
|
|
|
- * @see Assert#hasLength(String, String)
|
|
|
- * @see Assert#hasText(String, String)
|
|
|
- * @see Assert#isTrue(boolean, String)
|
|
|
- * @see Assert#isNull(Object, String)
|
|
|
- * @see Assert#notNull(Object, String)
|
|
|
- * @param exception 参数校验异常
|
|
|
- * @return API返回结果对象包装后的错误输出结果
|
|
|
- */
|
|
|
- @ExceptionHandler(IllegalArgumentException.class)
|
|
|
- @ResponseStatus(HttpStatus.OK)
|
|
|
- public R handleIllegalArgumentException(IllegalArgumentException exception) {
|
|
|
- log.error("非法参数,ex = {}", exception.getMessage(), exception);
|
|
|
- return R.failed(exception.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * AccessDeniedException
|
|
|
- * @param e the e
|
|
|
- * @return R
|
|
|
- */
|
|
|
- @ExceptionHandler(AccessDeniedBizException.class)
|
|
|
- @ResponseStatus(HttpStatus.FORBIDDEN)
|
|
|
- public R handleAccessDeniedException(AccessDeniedBizException e) {
|
|
|
- log.warn("拒绝授权异常信息 ex={}", "Access is denied");
|
|
|
- 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(String.format("%s %s", fieldErrors.get(0).getField(), 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());
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * validationCode Exception
|
|
|
- *
|
|
|
- * @param exception 异常
|
|
|
- * @return R
|
|
|
- */
|
|
|
- @ExceptionHandler({ValidateCodeException.class })
|
|
|
- @ResponseStatus(HttpStatus.OK)
|
|
|
- public R bindValidateCodeHandler(ValidateCodeException exception) {
|
|
|
- log.warn("验证码异常:{}", exception.getMessage());
|
|
|
- return R.failed(exception.getMessage());
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * aes 解密 Exception
|
|
|
- *
|
|
|
- * @param exception 异常
|
|
|
- * @return R
|
|
|
- */
|
|
|
- @ExceptionHandler({AesDecodeException.class })
|
|
|
- @ResponseStatus(HttpStatus.OK)
|
|
|
- public R<Object> bindAesDecodeException(AesDecodeException exception) {
|
|
|
- log.error("解密异常:{}", exception.getMessage(),exception);
|
|
|
- return R.failed(exception.getMessage());
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 全局异常.
|
|
|
+ * @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);
|
|
|
+
|
|
|
+ return R.failed(e.getLocalizedMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 运行时异常.
|
|
|
+ * @param e the e
|
|
|
+ * @return R
|
|
|
+ */
|
|
|
+ @ExceptionHandler(RuntimeException.class)
|
|
|
+ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
+ public R handleGlobalRuntimeException(RuntimeException e) {
|
|
|
+ log.error("运行时异常 ex={}", e.getMessage(), e);
|
|
|
+
|
|
|
+ return R.failed(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理业务校验过程中碰到的非法参数异常 该异常基本由{@link Assert}抛出
|
|
|
+ * @see Assert#hasLength(String, String)
|
|
|
+ * @see Assert#hasText(String, String)
|
|
|
+ * @see Assert#isTrue(boolean, String)
|
|
|
+ * @see Assert#isNull(Object, String)
|
|
|
+ * @see Assert#notNull(Object, String)
|
|
|
+ * @param exception 参数校验异常
|
|
|
+ * @return API返回结果对象包装后的错误输出结果
|
|
|
+ */
|
|
|
+ @ExceptionHandler(IllegalArgumentException.class)
|
|
|
+ @ResponseStatus(HttpStatus.OK)
|
|
|
+ public R handleIllegalArgumentException(IllegalArgumentException exception) {
|
|
|
+ log.error("非法参数,ex = {}", exception.getMessage(), exception);
|
|
|
+ return R.failed(exception.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * AccessDeniedException
|
|
|
+ * @param e the e
|
|
|
+ * @return R
|
|
|
+ */
|
|
|
+ @ExceptionHandler(AccessDeniedBizException.class)
|
|
|
+ @ResponseStatus(HttpStatus.FORBIDDEN)
|
|
|
+ public R handleAccessDeniedException(AccessDeniedBizException e) {
|
|
|
+ log.warn("拒绝授权异常信息 ex={}", "Access is denied");
|
|
|
+ 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(String.format("%s %s", fieldErrors.get(0).getField(), 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());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * validationCode Exception
|
|
|
+ * @param exception 异常
|
|
|
+ * @return R
|
|
|
+ */
|
|
|
+ @ExceptionHandler({ ValidateCodeException.class })
|
|
|
+ @ResponseStatus(HttpStatus.OK)
|
|
|
+ public R bindValidateCodeHandler(ValidateCodeException exception) {
|
|
|
+ log.warn("验证码异常:{}", exception.getMessage());
|
|
|
+ return R.failed(exception.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * aes 解密 Exception
|
|
|
+ * @param exception 异常
|
|
|
+ * @return R
|
|
|
+ */
|
|
|
+ @ExceptionHandler({ AesDecodeException.class })
|
|
|
+ @ResponseStatus(HttpStatus.OK)
|
|
|
+ public R<Object> bindAesDecodeException(AesDecodeException exception) {
|
|
|
+ log.error("解密异常:{}", exception.getMessage(), exception);
|
|
|
+ return R.failed(exception.getMessage());
|
|
|
+ }
|
|
|
|
|
|
}
|