|
@@ -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());
|
|
|
+ }
|
|
|
}
|