Wrap underlying checked exception with IllegalStateException. Add tests.
This commit is contained in:
@@ -113,6 +113,9 @@ class FeignCircuitBreakerInvocationHandler implements InvocationHandler {
|
||||
if (underlyingException instanceof RuntimeException) {
|
||||
throw (RuntimeException) underlyingException;
|
||||
}
|
||||
if (underlyingException != null) {
|
||||
throw new IllegalStateException(underlyingException);
|
||||
}
|
||||
throw new IllegalStateException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.openfeign.circuitbreaker;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
@@ -49,6 +50,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
@@ -65,6 +67,9 @@ public class CircuitBreakerTests {
|
||||
@Autowired
|
||||
TestClient testClient;
|
||||
|
||||
@Autowired
|
||||
ExceptionClient exceptionClient;
|
||||
|
||||
@Autowired
|
||||
TestClientWithFactory testClientWithFactory;
|
||||
|
||||
@@ -111,6 +116,17 @@ public class CircuitBreakerTests {
|
||||
assertThat(testClientWithFactory.getException()).isEqualTo("Fixed response");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRuntimeExceptionUnwrapped() {
|
||||
assertThatExceptionOfType(UnsupportedOperationException.class)
|
||||
.isThrownBy(() -> exceptionClient.getRuntimeException());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCheckedExceptionWrapped() {
|
||||
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() -> exceptionClient.getCheckedException());
|
||||
}
|
||||
|
||||
@FeignClient(name = "test", url = "http://localhost:${server.port}/", fallback = Fallback.class)
|
||||
protected interface TestClient {
|
||||
|
||||
@@ -122,6 +138,18 @@ public class CircuitBreakerTests {
|
||||
|
||||
}
|
||||
|
||||
@FeignClient(name = "exceptionClient", url = "http://localhost:${server.port}/",
|
||||
fallbackFactory = ExceptionThrowingFallbackFactory.class)
|
||||
protected interface ExceptionClient {
|
||||
|
||||
@GetMapping("/runtimeException")
|
||||
Hello getRuntimeException();
|
||||
|
||||
@GetMapping("/runtimeException")
|
||||
Hello getCheckedException() throws IOException;
|
||||
|
||||
}
|
||||
|
||||
@Component
|
||||
static class Fallback implements TestClient {
|
||||
|
||||
@@ -159,6 +187,25 @@ public class CircuitBreakerTests {
|
||||
|
||||
}
|
||||
|
||||
static class ExceptionThrowingFallbackFactory implements FallbackFactory<ExceptionClient> {
|
||||
|
||||
@Override
|
||||
public ExceptionClient create(Throwable cause) {
|
||||
return new ExceptionClient() {
|
||||
@Override
|
||||
public Hello getRuntimeException() {
|
||||
throw new UnsupportedOperationException("Not implemented!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Hello getCheckedException() throws IOException {
|
||||
throw new IOException();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static class FallbackWithFactory implements TestClientWithFactory {
|
||||
|
||||
@Override
|
||||
@@ -176,7 +223,7 @@ public class CircuitBreakerTests {
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableAutoConfiguration
|
||||
@RestController
|
||||
@EnableFeignClients(clients = { TestClient.class, TestClientWithFactory.class })
|
||||
@EnableFeignClients(clients = { TestClient.class, TestClientWithFactory.class, ExceptionClient.class })
|
||||
@Import(NoSecurityConfiguration.class)
|
||||
protected static class Application implements TestClient {
|
||||
|
||||
@@ -228,6 +275,11 @@ public class CircuitBreakerTests {
|
||||
return new TestFallbackFactory();
|
||||
}
|
||||
|
||||
@Bean
|
||||
ExceptionThrowingFallbackFactory exceptionThrowingFallbackFactory() {
|
||||
return new ExceptionThrowingFallbackFactory();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user