|
@@ -0,0 +1,133 @@
|
|
|
|
+package com.persagy.fm.common.exception;
|
|
|
|
+
|
|
|
|
+import com.persagy.common.enums.ResponseCode;
|
|
|
|
+import com.persagy.common.exception.BusinessException;
|
|
|
|
+import com.persagy.fm.common.response.FmResponseMsg;
|
|
|
|
+import com.persagy.fm.common.response.FmResponseUtil;
|
|
|
|
+import feign.FeignException;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
|
+import org.springframework.http.converter.HttpMessageNotReadableException;
|
|
|
|
+import org.springframework.validation.BindException;
|
|
|
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
+import org.springframework.web.bind.annotation.ResponseStatus;
|
|
|
|
+import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
|
+import org.springframework.web.servlet.NoHandlerFoundException;
|
|
|
|
+
|
|
|
|
+import javax.validation.ValidationException;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @description: 通用异常处理类
|
|
|
|
+ * @author: lixing
|
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
|
+ * @since: 2021/3/5 5:10 下午
|
|
|
|
+ * @version: V1.0
|
|
|
|
+ **/
|
|
|
|
+@RestControllerAdvice
|
|
|
|
+@Slf4j
|
|
|
|
+public class FmExceptionHandler {
|
|
|
|
+ /**
|
|
|
|
+ * feign调用异常
|
|
|
|
+ */
|
|
|
|
+ @ExceptionHandler({RpcException.class})
|
|
|
|
+ public FmResponseMsg handleRpcException(RpcException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ return FmResponseUtil.errorMsg(ResponseCode.C0110.getCode(), e.getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 空指针异常
|
|
|
|
+ */
|
|
|
|
+ @ExceptionHandler(NullPointerException.class)
|
|
|
|
+ public FmResponseMsg handleBusinessRuntimeException(NullPointerException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ return FmResponseUtil.errorMsg(ResponseCode.B0001.getCode(), e.getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 自定义业务异常
|
|
|
|
+ */
|
|
|
|
+ @ExceptionHandler(BusinessException.class)
|
|
|
|
+ public FmResponseMsg handleBusinessRuntimeException(BusinessException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ return FmResponseUtil.errorMsg(e.getErrorCode(), e.getErrorDesc());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 入参校验异常
|
|
|
|
+ */
|
|
|
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
|
+ public FmResponseMsg handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ return FmResponseUtil.errorMsg(ResponseCode.A0400.getCode(), e.getBindingResult().getFieldError().getDefaultMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 入参校验异常
|
|
|
|
+ */
|
|
|
|
+ @ExceptionHandler(BindException.class)
|
|
|
|
+ public FmResponseMsg handleBindException(BindException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ return FmResponseUtil.errorMsg(ResponseCode.A0400.getCode(), e.getBindingResult().getFieldError().getDefaultMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 参数校验异常,message 为手动抛出时定义的具体异常信息
|
|
|
|
+ */
|
|
|
|
+ @ExceptionHandler(ValidationException.class)
|
|
|
|
+ public FmResponseMsg handleValidationException(ValidationException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ return FmResponseUtil.errorMsg(ResponseCode.A0400.getCode(), e.getCause().getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 参数校验异常,message 为手动抛出时定义的具体异常信息
|
|
|
|
+ */
|
|
|
|
+ @ExceptionHandler(IllegalArgumentException.class)
|
|
|
|
+ public FmResponseMsg handleIllegalArgumentException(IllegalArgumentException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ return FmResponseUtil.errorMsg(ResponseCode.A0400.getCode(), e.getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * URI请求链接没有映射
|
|
|
|
+ */
|
|
|
|
+ @ResponseStatus(HttpStatus.NOT_FOUND)
|
|
|
|
+ @ExceptionHandler(NoHandlerFoundException.class)
|
|
|
|
+ public FmResponseMsg handleNoHandlerFoundException(NoHandlerFoundException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ return FmResponseUtil.errorMsg(ResponseCode.A0422.getCode(), e.getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Feign 调用异常
|
|
|
|
+ */
|
|
|
|
+ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
+ @ExceptionHandler(FeignException.class)
|
|
|
|
+ public FmResponseMsg handleFeignException(FeignException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ return FmResponseUtil.errorMsg(ResponseCode.C0110.getCode(), e.getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * body体未找到
|
|
|
|
+ */
|
|
|
|
+ @ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
|
+ @ExceptionHandler(HttpMessageNotReadableException.class)
|
|
|
|
+ public FmResponseMsg handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ return FmResponseUtil.errorMsg(ResponseCode.A0402.getCode(), ResponseCode.A0402.getDesc());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 系统默认繁忙
|
|
|
|
+ */
|
|
|
|
+ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
+ @ExceptionHandler(Exception.class)
|
|
|
|
+ public FmResponseMsg handleException(Exception e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ return FmResponseUtil.errorMsg(ResponseCode.Z9999.getCode(), ResponseCode.Z9999.getDesc());
|
|
|
|
+ }
|
|
|
|
+}
|