Browse Source

增加异常处理

yq 2 years ago
parent
commit
d9dcffdfb7

+ 2 - 2
base-modules/service-system/service-system-api/src/main/java/com/usky/system/RemoteTenantService.java

@@ -1,6 +1,6 @@
 package com.usky.system;
 
-import com.usky.system.factory.RemoteUserFallbackFactory;
+import com.usky.system.factory.RemoteTenantFallbackFactory;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -11,7 +11,7 @@ import java.util.List;
  * @author yq
  * @date 2022/7/12 13:13
  */
-@FeignClient(contextId = "remoteTenantService", value = "usky-system", fallbackFactory = RemoteUserFallbackFactory.class)
+@FeignClient(contextId = "remoteTenantService", value = "usky-system", fallbackFactory = RemoteTenantFallbackFactory.class)
 public interface RemoteTenantService {
 
     /**

+ 3 - 2
base-modules/service-system/service-system-api/src/main/java/com/usky/system/factory/RemoteTenantFallbackFactory.java

@@ -1,5 +1,6 @@
 package com.usky.system.factory;
 
+import com.usky.common.core.exception.FeignBadRequestException;
 import com.usky.system.RemoteTenantService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -25,12 +26,12 @@ public class RemoteTenantFallbackFactory implements FallbackFactory<RemoteTenant
 
             @Override
             public List<Integer> getTenantIds() {
-                return null;
+                throw new FeignBadRequestException(500,"获取租户异常"+throwable.getMessage());
             }
 
             @Override
             public Boolean validTenant(Integer id) {
-                return false;
+                throw new FeignBadRequestException(500,"校验租户异常"+throwable.getMessage());
             }
         };
     }

+ 6 - 0
usky-common/usky-common-core/pom.xml

@@ -102,6 +102,12 @@
             <artifactId>hutool-all</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
+            <version>2.2.10.RELEASE</version>
+        </dependency>
+
     </dependencies>
 
 </project>

+ 15 - 0
usky-common/usky-common-core/src/main/java/com/usky/common/core/exception/FeignBadRequestException.java

@@ -0,0 +1,15 @@
+package com.usky.common.core.exception;
+
+import com.netflix.hystrix.exception.HystrixBadRequestException;
+
+public class FeignBadRequestException extends HystrixBadRequestException {
+    private int code;
+
+    public FeignBadRequestException(int code, String message){
+        super(message);
+        this.code = code;
+    }
+    public int getCode(){
+        return code;
+    }
+}

+ 13 - 1
usky-common/usky-common-core/src/main/java/com/usky/common/core/handler/UnifiedExceptionHandler.java

@@ -6,7 +6,6 @@ import com.usky.common.core.exception.*;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
 import org.springframework.http.HttpStatus;
 import org.springframework.http.converter.HttpMessageNotReadableException;
 import org.springframework.util.CollectionUtils;
@@ -19,6 +18,7 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.context.request.WebRequest;
 import org.springframework.web.servlet.NoHandlerFoundException;
 import org.yaml.snakeyaml.constructor.DuplicateKeyException;
 
@@ -245,6 +245,18 @@ public class UnifiedExceptionHandler {
         return handleBusinessException(be);
     }
 
+    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
+    @ResponseBody
+    @ExceptionHandler(value = {FeignBadRequestException.class})
+    public ApiResult<Void> handleFeignException(FeignBadRequestException ex, WebRequest request) {
+        ErrorCode errorCode = ErrorCode.create(SystemErrorCode.SYS_SYSTEM_FEIGN_RESULT_DECODE_ERROR.getCode(),
+                SystemErrorCode.SYS_SYSTEM_FEIGN_RESULT_DECODE_ERROR.getDefaultMessage());
+        BusinessException be = new BusinessException(errorCode, ex.getMessage());
+        return handleBusinessException(be);
+    }
+
+
+
     private void logError(ErrorCode errorCode, Throwable t, boolean isSystemError) {
         String logTemplate = isSystemError ? SYS_ERROR_LOG_MESSAGE_TEMPLATE : BIZ_ERROR_LOG_MESSAGE_TEMPLATE;
         LOGGER.error(String.format(logTemplate,

+ 1 - 3
usky-common/usky-common-security/src/main/java/com/usky/common/security/feign/OpenFeignErrorDecoder.java

@@ -8,7 +8,6 @@ import feign.Util;
 import feign.codec.ErrorDecoder;
 import lombok.extern.slf4j.Slf4j;
 
-import java.io.IOException;
 import java.nio.charset.Charset;
 
 @Slf4j
@@ -30,8 +29,7 @@ public class OpenFeignErrorDecoder implements ErrorDecoder {
             if(!resultData.isSuccess()){
                 return new BusinessException(resultData.getMsg());
             }
-
-        } catch (IOException e) {
+        } catch (Exception e) {
             e.printStackTrace();
         }