fix fallback ex

pull/1021/head
seanyu 2 years ago
parent 0d206bb271
commit 611ca23619

@ -0,0 +1,10 @@
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;
@ -119,7 +120,11 @@ 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) {
throw e.getCause();
}
} }
private void unwrapAndRethrow(Exception exception) { private void unwrapAndRethrow(Exception 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,51 @@ 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) {
); Throwable underlyingException = e.getCause();
if (underlyingException instanceof IllegalStateException) {
throw e;
}
if (underlyingException instanceof RuntimeException) {
throw (RuntimeException) underlyingException;
}
throw new IllegalStateException(underlyingException);
}
} }
} }

Loading…
Cancel
Save