diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerInvocationHandler.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerInvocationHandler.java index dcacc306a..abcb47586 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerInvocationHandler.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisFeignCircuitBreakerInvocationHandler.java @@ -122,7 +122,9 @@ public class PolarisFeignCircuitBreakerInvocationHandler implements InvocationHa } try { return circuitBreaker.run(supplier, fallbackFunction); - } catch (FallbackWrapperException e) { + } + catch (FallbackWrapperException e) { + // unwrap And Rethrow throw e.getCause(); } } @@ -134,9 +136,9 @@ public class PolarisFeignCircuitBreakerInvocationHandler implements InvocationHa throw (RuntimeException) underlyingException; } if (underlyingException != null) { - throw new IllegalStateException(underlyingException); + throw new FallbackWrapperException(underlyingException); } - throw new IllegalStateException(exception); + throw new FallbackWrapperException(exception); } } diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateInterceptor.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateInterceptor.java index 42215c007..8cb26902f 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateInterceptor.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateInterceptor.java @@ -98,11 +98,10 @@ public class PolarisCircuitBreakerRestTemplateInterceptor implements ClientHttpR throw new FallbackWrapperException(t); } ); - } catch (FallbackWrapperException e) { + } + catch (FallbackWrapperException e) { + // unwrap And Rethrow Throwable underlyingException = e.getCause(); - if (underlyingException instanceof IllegalStateException) { - throw e; - } if (underlyingException instanceof RuntimeException) { throw (RuntimeException) underlyingException; } diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerFeignIntegrationTest.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerFeignIntegrationTest.java index cdde3388f..ea77505eb 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerFeignIntegrationTest.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisCircuitBreakerFeignIntegrationTest.java @@ -111,7 +111,7 @@ public class PolarisCircuitBreakerFeignIntegrationTest { Utils.sleepUninterrupted(2000); assertThatThrownBy(() -> { echoService.echo(null); - }).isInstanceOf(Exception.class); + }).isInstanceOf(InvocationTargetException.class); assertThatThrownBy(() -> { fooService.echo("test"); }).isInstanceOf(NoFallbackAvailableException.class); @@ -244,7 +244,7 @@ public class PolarisCircuitBreakerFeignIntegrationTest { @Override public String echo(@RequestParam("str") String param) throws InvocationTargetException { if (param == null) { - throw new InvocationTargetException(new Exception()); + throw new InvocationTargetException(new Exception(), "test InvocationTargetException"); } return "echo fallback"; } diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateIntegrationTest.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateIntegrationTest.java index 1d12a2dc0..2080b63c8 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateIntegrationTest.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/test/java/com/tencent/cloud/polaris/circuitbreaker/resttemplate/PolarisCircuitBreakerRestTemplateIntegrationTest.java @@ -75,6 +75,7 @@ import org.springframework.web.util.DefaultUriBuilderFactory; import static com.tencent.polaris.test.common.Consts.NAMESPACE_TEST; import static com.tencent.polaris.test.common.TestUtils.SERVER_ADDRESS_ENV; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; @@ -142,12 +143,14 @@ public class PolarisCircuitBreakerRestTemplateIntegrationTest { assertThat(defaultRestTemplate.getForObject("http://localhost:18001/example/service/b/info", String.class)).isEqualTo("fallback"); mockServer.verify(); mockServer.reset(); + assertThatThrownBy(() -> { + restTemplateFallbackFromPolaris.getForObject("/example/service/b/info", String.class); + }).isInstanceOf(IllegalStateException.class); assertThat(restTemplateFallbackFromCode.getForObject("/example/service/b/info", String.class)).isEqualTo("\"this is a fallback class\""); Utils.sleepUninterrupted(2000); assertThat(restTemplateFallbackFromCode2.getForObject("/example/service/b/info", String.class)).isEqualTo("\"this is a fallback class\""); Utils.sleepUninterrupted(2000); - assertThat(restTemplateFallbackFromCode3.getForEntity("/example/service/b/info", String.class) - .getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(restTemplateFallbackFromCode3.getForEntity("/example/service/b/info", String.class).getStatusCode()).isEqualTo(HttpStatus.OK); Utils.sleepUninterrupted(2000); assertThat(restTemplateFallbackFromCode4.getForObject("/example/service/b/info", String.class)).isEqualTo("fallback"); Utils.sleepUninterrupted(2000);