Unwarap InvocationTargetException and NoFallbackAvailableException.
This commit is contained in:
+15
-2
@@ -17,6 +17,7 @@
|
|||||||
package org.springframework.cloud.openfeign;
|
package org.springframework.cloud.openfeign;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationHandler;
|
import java.lang.reflect.InvocationHandler;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Proxy;
|
import java.lang.reflect.Proxy;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
@@ -29,6 +30,7 @@ import feign.Target;
|
|||||||
|
|
||||||
import org.springframework.cloud.client.circuitbreaker.CircuitBreaker;
|
import org.springframework.cloud.client.circuitbreaker.CircuitBreaker;
|
||||||
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
|
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
|
||||||
|
import org.springframework.cloud.client.circuitbreaker.NoFallbackAvailableException;
|
||||||
import org.springframework.web.context.request.RequestAttributes;
|
import org.springframework.web.context.request.RequestAttributes;
|
||||||
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
|
||||||
@@ -95,15 +97,26 @@ class FeignCircuitBreakerInvocationHandler implements InvocationHandler {
|
|||||||
try {
|
try {
|
||||||
return this.fallbackMethodMap.get(method).invoke(fallback, args);
|
return this.fallbackMethodMap.get(method).invoke(fallback, args);
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception exception) {
|
||||||
throw new IllegalStateException(e);
|
unwrapAndRethrow(exception);
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
};
|
};
|
||||||
return circuitBreaker.run(supplier, fallbackFunction);
|
return circuitBreaker.run(supplier, fallbackFunction);
|
||||||
}
|
}
|
||||||
return circuitBreaker.run(supplier);
|
return circuitBreaker.run(supplier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void unwrapAndRethrow(Exception exception) {
|
||||||
|
if (exception instanceof InvocationTargetException || exception instanceof NoFallbackAvailableException) {
|
||||||
|
Throwable underlyingException = exception.getCause();
|
||||||
|
if (underlyingException instanceof RuntimeException) {
|
||||||
|
throw (RuntimeException) underlyingException;
|
||||||
|
}
|
||||||
|
throw new IllegalStateException(exception);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Supplier<Object> asSupplier(final Method method, final Object[] args) {
|
private Supplier<Object> asSupplier(final Method method, final Object[] args) {
|
||||||
final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
||||||
return () -> {
|
return () -> {
|
||||||
|
|||||||
Reference in New Issue
Block a user