|
|
@@ -0,0 +1,78 @@
|
|
|
+package com.mrxu.framework.boot.handle;
|
|
|
+
|
|
|
+
|
|
|
+import com.mrxu.framework.boot.feign.FeignProviderException;
|
|
|
+import com.mrxu.framework.boot.web.ServletUtils;
|
|
|
+import com.mrxu.framework.common.util.BaseCode;
|
|
|
+import com.mrxu.framework.common.util.BusinessException;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.validation.BindingResult;
|
|
|
+import org.springframework.validation.FieldError;
|
|
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
+import org.springframework.web.bind.MissingServletRequestParameterException;
|
|
|
+import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
+import org.springframework.web.bind.annotation.ResponseStatus;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author: xujunwei
|
|
|
+ * @Description: 此异常拦截给微服务提供方使用,不能与WebExceptionHandler 同时使用
|
|
|
+ */
|
|
|
+@ControllerAdvice
|
|
|
+@RestController
|
|
|
+@Slf4j
|
|
|
+public class FeignProviderExceptionHandler {
|
|
|
+
|
|
|
+ /*@Retention(RetentionPolicy.RUNTIME)
|
|
|
+ @Target({ElementType.TYPE})
|
|
|
+ @Documented
|
|
|
+ @Import({FeignProviderExceptionHandler.class})
|
|
|
+ public @interface EnableFeignProviderExceptionHandler {
|
|
|
+ }*/
|
|
|
+
|
|
|
+ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
+ @ExceptionHandler(Exception.class)
|
|
|
+ public Object handleException(Exception e) {
|
|
|
+ log.error("系统错误:{}",e.getMessage(), e);
|
|
|
+ return rendError(false,BaseCode.ERROR);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
+ @ExceptionHandler(BusinessException.class)
|
|
|
+ public Object handleLogicalException(BusinessException e) {
|
|
|
+ log.warn(e.getMessage());
|
|
|
+ return rendError(true,new BaseCode(e.getCode(),e.getMsg()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
+ public Object handleValidException(MethodArgumentNotValidException e) {
|
|
|
+ BindingResult result = e.getBindingResult();
|
|
|
+ FieldError error = result.getFieldError();
|
|
|
+ log.warn("请求参数错误:{}",error.getDefaultMessage());
|
|
|
+ return rendError(true,BaseCode.ERR_PARAMS_VALID);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 缺少必要参数
|
|
|
+ * @param e
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
+ @ExceptionHandler(MissingServletRequestParameterException.class)
|
|
|
+ public Object handleValidException(MissingServletRequestParameterException e) {
|
|
|
+ log.warn("请求缺少参数:{}",e.getMessage());
|
|
|
+ return rendError(true,BaseCode.ERR_PARAMS_MISS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object rendError(boolean isBusinessException,BaseCode code) {
|
|
|
+ HttpServletRequest request = ServletUtils.getRequest();
|
|
|
+ FeignProviderException exception = new FeignProviderException(isBusinessException,code);
|
|
|
+ return exception;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|