fix custom fallback exception.

pull/1080/head
Shanyou Yu (Sean Yu) 2 years ago committed by Haotian Zhang
parent a5715f65ef
commit 5ec0f04252

@ -0,0 +1,27 @@
/*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.circuitbreaker.exception;
public class FallbackWrapperException extends RuntimeException {
public FallbackWrapperException(Throwable cause) {
super(cause);
}
}

@ -25,6 +25,7 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.polaris.circuitbreaker.exception.FallbackWrapperException;
import com.tencent.polaris.api.pojo.CircuitBreakerStatus; import com.tencent.polaris.api.pojo.CircuitBreakerStatus;
import com.tencent.polaris.circuitbreak.client.exception.CallAbortedException; import com.tencent.polaris.circuitbreak.client.exception.CallAbortedException;
import feign.Request; import feign.Request;
@ -87,11 +88,11 @@ public class PolarisCircuitBreakerFallbackFactory implements FallbackFactory {
return decoder.decode(response, method.getGenericReturnType()); return decoder.decode(response, method.getGenericReturnType());
} }
catch (IOException e) { catch (IOException e) {
throw new IllegalStateException(e); throw new FallbackWrapperException(e);
} }
} }
} }
throw new IllegalStateException(t); throw new FallbackWrapperException(t);
} }
} }
} }

@ -26,6 +26,7 @@ import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import com.tencent.cloud.polaris.circuitbreaker.exception.FallbackWrapperException;
import feign.InvocationHandlerFactory; import feign.InvocationHandlerFactory;
import feign.Target; import feign.Target;
import feign.codec.Decoder; import feign.codec.Decoder;
@ -136,7 +137,13 @@ public class PolarisFeignCircuitBreakerInvocationHandler implements InvocationHa
return fallback.fallback(method); return fallback.fallback(method);
}; };
} }
return circuitBreaker.run(supplier, fallbackFunction); try {
return circuitBreaker.run(supplier, fallbackFunction);
}
catch (FallbackWrapperException e) {
// unwrap And Rethrow
throw e.getCause();
}
} }
private void unwrapAndRethrow(Exception exception) { private void unwrapAndRethrow(Exception exception) {
@ -146,9 +153,9 @@ public class PolarisFeignCircuitBreakerInvocationHandler implements InvocationHa
throw (RuntimeException) underlyingException; throw (RuntimeException) underlyingException;
} }
if (underlyingException != null) { if (underlyingException != null) {
throw new IllegalStateException(underlyingException); throw new FallbackWrapperException(underlyingException);
} }
throw new IllegalStateException(exception); throw new FallbackWrapperException(exception);
} }
} }

@ -20,6 +20,7 @@ package com.tencent.cloud.polaris.circuitbreaker.resttemplate;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import com.tencent.cloud.polaris.circuitbreaker.exception.FallbackWrapperException;
import com.tencent.polaris.api.pojo.CircuitBreakerStatus; import com.tencent.polaris.api.pojo.CircuitBreakerStatus;
import com.tencent.polaris.circuitbreak.client.exception.CallAbortedException; import com.tencent.polaris.circuitbreak.client.exception.CallAbortedException;
@ -63,39 +64,50 @@ public class PolarisCircuitBreakerRestTemplateInterceptor implements ClientHttpR
@Override @Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
return circuitBreakerFactory.create(request.getURI().getHost() + "#" + request.getURI().getPath()).run( try {
() -> { return circuitBreakerFactory.create(request.getURI().getHost() + "#" + request.getURI().getPath()).run(
try { () -> {
ClientHttpResponse response = execution.execute(request, body); try {
ResponseErrorHandler errorHandler = restTemplate.getErrorHandler(); ClientHttpResponse response = execution.execute(request, body);
if (errorHandler.hasError(response)) { ResponseErrorHandler errorHandler = restTemplate.getErrorHandler();
errorHandler.handleError(request.getURI(), request.getMethod(), response); if (errorHandler.hasError(response)) {
errorHandler.handleError(request.getURI(), request.getMethod(), response);
}
return response;
} }
return response; catch (IOException e) {
} throw new IllegalStateException(e);
catch (IOException e) { }
throw new IllegalStateException(e); },
} t -> {
}, if (StringUtils.hasText(polarisCircuitBreaker.fallback())) {
t -> { CircuitBreakerStatus.FallbackInfo fallbackInfo = new CircuitBreakerStatus.FallbackInfo(200, null, polarisCircuitBreaker.fallback());
if (StringUtils.hasText(polarisCircuitBreaker.fallback())) {
CircuitBreakerStatus.FallbackInfo fallbackInfo = new CircuitBreakerStatus.FallbackInfo(200, null, polarisCircuitBreaker.fallback());
return new PolarisCircuitBreakerHttpResponse(fallbackInfo);
}
if (!PolarisCircuitBreakerFallback.class.toGenericString().equals(polarisCircuitBreaker.fallbackClass().toGenericString())) {
Method method = ReflectionUtils.findMethod(PolarisCircuitBreakerFallback.class, "fallback");
PolarisCircuitBreakerFallback polarisCircuitBreakerFallback = applicationContext.getBean(polarisCircuitBreaker.fallbackClass());
return (PolarisCircuitBreakerHttpResponse) ReflectionUtils.invokeMethod(method, polarisCircuitBreakerFallback);
}
if (t instanceof CallAbortedException) {
CircuitBreakerStatus.FallbackInfo fallbackInfo = ((CallAbortedException) t).getFallbackInfo();
if (fallbackInfo != null) {
return new PolarisCircuitBreakerHttpResponse(fallbackInfo); return new PolarisCircuitBreakerHttpResponse(fallbackInfo);
} }
if (!PolarisCircuitBreakerFallback.class.toGenericString().equals(polarisCircuitBreaker.fallbackClass().toGenericString())) {
Method method = ReflectionUtils.findMethod(PolarisCircuitBreakerFallback.class, "fallback");
PolarisCircuitBreakerFallback polarisCircuitBreakerFallback = applicationContext.getBean(polarisCircuitBreaker.fallbackClass());
return (PolarisCircuitBreakerHttpResponse) ReflectionUtils.invokeMethod(method, polarisCircuitBreakerFallback);
}
if (t instanceof CallAbortedException) {
CircuitBreakerStatus.FallbackInfo fallbackInfo = ((CallAbortedException) t).getFallbackInfo();
if (fallbackInfo != null) {
return new PolarisCircuitBreakerHttpResponse(fallbackInfo);
}
}
throw new FallbackWrapperException(t);
} }
throw new IllegalStateException(t); );
} }
); catch (FallbackWrapperException e) {
// unwrap And Rethrow
Throwable underlyingException = e.getCause();
if (underlyingException instanceof RuntimeException) {
throw (RuntimeException) underlyingException;
}
throw new IllegalStateException(underlyingException);
}
} }
} }

@ -106,7 +106,7 @@ public class PolarisCircuitBreakerFeignIntegrationTest {
Utils.sleepUninterrupted(2000); Utils.sleepUninterrupted(2000);
assertThatThrownBy(() -> { assertThatThrownBy(() -> {
echoService.echo(null); echoService.echo(null);
}).isInstanceOf(Exception.class); }).isInstanceOf(InvocationTargetException.class);
assertThatThrownBy(() -> { assertThatThrownBy(() -> {
fooService.echo("test"); fooService.echo("test");
}).isInstanceOf(NoFallbackAvailableException.class); }).isInstanceOf(NoFallbackAvailableException.class);
@ -203,7 +203,7 @@ public class PolarisCircuitBreakerFeignIntegrationTest {
@Override @Override
public String echo(@RequestParam("str") String param) throws InvocationTargetException { public String echo(@RequestParam("str") String param) throws InvocationTargetException {
if (param == null) { if (param == null) {
throw new InvocationTargetException(new Exception()); throw new InvocationTargetException(new Exception(), "test InvocationTargetException");
} }
return "echo fallback"; return "echo fallback";
} }

@ -69,6 +69,7 @@ import org.springframework.web.util.DefaultUriBuilderFactory;
import static com.tencent.polaris.test.common.TestUtils.SERVER_ADDRESS_ENV; 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.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; 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.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
@ -139,12 +140,14 @@ public class PolarisCircuitBreakerIntegrationTest {
assertThat(defaultRestTemplate.getForObject("http://localhost:18001/example/service/b/info", String.class)).isEqualTo("fallback"); assertThat(defaultRestTemplate.getForObject("http://localhost:18001/example/service/b/info", String.class)).isEqualTo("fallback");
mockServer.verify(); mockServer.verify();
mockServer.reset(); 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\""); assertThat(restTemplateFallbackFromCode.getForObject("/example/service/b/info", String.class)).isEqualTo("\"this is a fallback class\"");
Utils.sleepUninterrupted(2000); Utils.sleepUninterrupted(2000);
assertThat(restTemplateFallbackFromCode2.getForObject("/example/service/b/info", String.class)).isEqualTo("\"this is a fallback class\""); assertThat(restTemplateFallbackFromCode2.getForObject("/example/service/b/info", String.class)).isEqualTo("\"this is a fallback class\"");
Utils.sleepUninterrupted(2000); Utils.sleepUninterrupted(2000);
assertThat(restTemplateFallbackFromCode3.getForEntity("/example/service/b/info", String.class) assertThat(restTemplateFallbackFromCode3.getForEntity("/example/service/b/info", String.class).getStatusCode()).isEqualTo(HttpStatus.OK);
.getStatusCode()).isEqualTo(HttpStatus.OK);
Utils.sleepUninterrupted(2000); Utils.sleepUninterrupted(2000);
assertThat(restTemplateFallbackFromCode4.getForObject("/example/service/b/info", String.class)).isEqualTo("fallback"); assertThat(restTemplateFallbackFromCode4.getForObject("/example/service/b/info", String.class)).isEqualTo("fallback");
Utils.sleepUninterrupted(2000); Utils.sleepUninterrupted(2000);

Loading…
Cancel
Save