use of javax.ws.rs.ServiceUnavailableException in project pulsar by apache.
the class BaseResource method getApiException.
public PulsarAdminException getApiException(Throwable e) {
if (e instanceof PulsarAdminException) {
return (PulsarAdminException) e;
} else if (e instanceof ServiceUnavailableException) {
if (e.getCause() instanceof java.net.ConnectException) {
return new ConnectException(e.getCause());
} else {
ServerErrorException see = (ServerErrorException) e;
int statusCode = see.getResponse().getStatus();
String httpError = getReasonFromServer(see);
return new PulsarAdminException(e, httpError, statusCode);
}
} else if (e instanceof WebApplicationException) {
// Handle 5xx exceptions
if (e instanceof ServerErrorException) {
ServerErrorException see = (ServerErrorException) e;
int statusCode = see.getResponse().getStatus();
String httpError = getReasonFromServer(see);
return new ServerSideErrorException(see, e.getMessage(), httpError, statusCode);
} else if (e instanceof ClientErrorException) {
// Handle 4xx exceptions
ClientErrorException cee = (ClientErrorException) e;
int statusCode = cee.getResponse().getStatus();
String httpError = getReasonFromServer(cee);
switch(statusCode) {
case 401:
case 403:
return new NotAuthorizedException(cee, httpError, statusCode);
case 404:
return new NotFoundException(cee, httpError, statusCode);
case 405:
return new NotAllowedException(cee, httpError, statusCode);
case 409:
return new ConflictException(cee, httpError, statusCode);
case 412:
return new PreconditionFailedException(cee, httpError, statusCode);
default:
return new PulsarAdminException(httpError, cee, httpError, statusCode);
}
} else {
WebApplicationException wae = (WebApplicationException) e;
int statusCode = wae.getResponse().getStatus();
String httpError = getReasonFromServer(wae);
return new PulsarAdminException(httpError, wae, httpError, statusCode);
}
} else {
return new PulsarAdminException(e);
}
}
use of javax.ws.rs.ServiceUnavailableException in project oci-java-sdk by oracle.
the class JaxRsCircuitBreakerImplTest method validateCircuitBreakerOpensTheCircuitWhenServiceUnavailableExceptionIsThrownAsync.
@Test
public void validateCircuitBreakerOpensTheCircuitWhenServiceUnavailableExceptionIsThrownAsync() {
List<Future<Response>> futures = new ArrayList<>();
for (int i = 0; i < MIN_NUM_CALLS + 2; i++) {
Supplier<Future<Response>> clientCall = circuitBreaker.decorateFuture(() -> {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
return responseFutureUnavailable;
});
Future<Response> responseFuture = clientCall.get();
futures.add(responseFuture);
}
int callCount = 0;
int unavailableCount = 0;
for (Future<Response> responseFuture : futures) {
try {
callCount++;
Response response = responseFuture.get();
System.out.println(response.getStatus());
} catch (CallNotAllowedException e) {
break;
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
} catch (ServiceUnavailableException e) {
unavailableCount++;
}
}
assertTrue(callCount >= MIN_NUM_CALLS);
assertTrue(unavailableCount >= MIN_NUM_CALLS);
assertEquals(CircuitBreaker.State.OPEN, circuitBreaker.getInternalCircuitBreaker().getState());
assertEquals(CircuitBreakerState.OPEN, circuitBreaker.getState());
}
use of javax.ws.rs.ServiceUnavailableException in project oci-java-sdk by oracle.
the class JaxRsCircuitBreakerImplTest method setup.
@Before
public void setup() throws ExecutionException, InterruptedException {
CircuitBreakerConfiguration config = CircuitBreakerConfiguration.builder().failureRateThreshold(50).permittedNumberOfCallsInHalfOpenState(4).slidingWindowSize(SLIDING_WINDOW_SIZE).minimumNumberOfCalls(MIN_NUM_CALLS).waitDurationInOpenState(Duration.ofSeconds(2)).build();
circuitBreaker = new JaxRsCircuitBreakerImpl(config);
Response response503 = mock(Response.class);
Mockito.when(response503.getStatus()).thenReturn(503);
Mockito.when(response503.getStatusInfo()).thenReturn(Response.Status.BAD_GATEWAY);
invocation503 = mock(Invocation.class);
Mockito.when(invocation503.invoke()).thenReturn(response503);
Mockito.when(responseFuture503.get()).thenReturn(response503);
Response response200 = mock(Response.class);
Mockito.when(response200.getStatus()).thenReturn(200);
Mockito.when(response200.getStatusInfo()).thenReturn(Response.Status.OK);
invocation200 = mock(Invocation.class);
Mockito.when(invocation200.invoke()).thenReturn(response200);
Mockito.when(responseFuture200.get()).thenReturn(response200);
invocationUnavailable = mock(Invocation.class);
Mockito.when(invocationUnavailable.invoke()).thenThrow(new ServiceUnavailableException());
invocationClientError = mock(Invocation.class);
Mockito.when(invocationClientError.invoke()).thenThrow(new ClientErrorException(Response.Status.NOT_ACCEPTABLE));
Mockito.when(responseFuture200.get()).thenReturn(response200);
Mockito.when(responseFuture503.get()).thenReturn(response503);
Mockito.when(responseFutureUnavailable.get()).thenThrow(new ServiceUnavailableException());
Response response409 = mock(Response.class);
Mockito.when(response409.getStatus()).thenReturn(409);
Mockito.when(response409.getStatusInfo()).thenReturn(buildIncorrectStateResponse());
invocation409 = mock(Invocation.class);
Mockito.when(invocation409.invoke()).thenReturn(response409);
Mockito.when(responseFuture409.get()).thenReturn(response409);
}
use of javax.ws.rs.ServiceUnavailableException in project oci-java-sdk by oracle.
the class CircuitBreakerConfigurationTest method validateCircuitBreakerConfigurationWithCustomValues.
@Test
public void validateCircuitBreakerConfigurationWithCustomValues() {
CircuitBreakerConfiguration config = CircuitBreakerConfiguration.builder().failureRateThreshold(50).slowCallRateThreshold(90).slowCallDurationThreshold(Duration.ofSeconds(6)).permittedNumberOfCallsInHalfOpenState(2).slidingWindowSize(10).minimumNumberOfCalls(4).waitDurationInOpenState(Duration.ofSeconds(2)).recordHttpStatuses(ImmutableSet.of(CircuitBreakerConfiguration.TOO_MANY_REQUESTS, CircuitBreakerConfiguration.SERVICE_UNAVAILABLE)).recordExceptions(ImmutableList.of(CircuitBreakerConfiguration.SERVICE_UNAVAILABLE_EXCEPTION_CLASS)).build();
JaxRsCircuitBreakerImpl circuitBreaker = new JaxRsCircuitBreakerImpl(config);
CircuitBreakerConfig internalConfig = circuitBreaker.getInternalCircuitBreakerConfig();
assertEquals(internalConfig.getFailureRateThreshold(), 50, 0.1);
assertEquals(internalConfig.getSlowCallRateThreshold(), 90, 0.1);
assertEquals(internalConfig.getSlowCallDurationThreshold(), Duration.ofSeconds(6));
assertEquals(internalConfig.getPermittedNumberOfCallsInHalfOpenState(), 2);
assertEquals(internalConfig.getSlidingWindowType(), CircuitBreakerConfig.SlidingWindowType.TIME_BASED);
assertEquals(internalConfig.getSlidingWindowSize(), 10);
assertEquals(internalConfig.getMinimumNumberOfCalls(), 4);
assertEquals(internalConfig.getWaitDurationInOpenState(), Duration.ofSeconds(2));
assertTrue(internalConfig.isAutomaticTransitionFromOpenToHalfOpenEnabled());
Response response503 = mock(Response.class);
Mockito.when(response503.getStatus()).thenReturn(503);
HttpStatusErrorException ex = new HttpStatusErrorException(response503);
Predicate<Throwable> recordExceptionPredicate = internalConfig.getRecordExceptionPredicate();
assertTrue(recordExceptionPredicate.test(ex));
assertTrue(recordExceptionPredicate.test(new ServiceUnavailableException()));
assertFalse(recordExceptionPredicate.test(new InternalServerErrorException()));
assertFalse(recordExceptionPredicate.test(new ProcessingException("test")));
Set<Integer> recordHttpStatuses = circuitBreaker.getRecordHttpStatuses();
assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.SERVICE_UNAVAILABLE));
assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.TOO_MANY_REQUESTS));
assertEquals(recordHttpStatuses.size(), 2);
}
use of javax.ws.rs.ServiceUnavailableException in project Europeana-Cloud by europeana.
the class DpsClient method manageResponse.
private <T> T manageResponse(ResponseParams<T> responseParameters, Supplier<Response> responseSupplier, String errorMessage) throws DpsException {
Response response = responseSupplier.get();
try {
response.bufferEntity();
if (responseParameters.isStatusCodeValid(response.getStatus())) {
return readEntityByClass(responseParameters, response);
} else if (response.getStatus() == HttpURLConnection.HTTP_UNAVAILABLE) {
throw DPSExceptionProvider.createException(errorMessage, "Service unavailable", new ServiceUnavailableException());
} else if (response.getStatus() == HttpURLConnection.HTTP_NOT_FOUND) {
throw DPSExceptionProvider.createException(errorMessage, "Endpoint not found", new NotFoundException());
}
ErrorInfo errorInfo = response.readEntity(ErrorInfo.class);
throw DPSExceptionProvider.createException(errorInfo);
} catch (DpsException | DPSClientException dpsException) {
// re-throw just created DpsException
throw dpsException;
} catch (ProcessingException processingException) {
String message = String.format("Could not deserialize response with statusCode: %d; message: %s", response.getStatus(), response.readEntity(String.class));
throw DPSExceptionProvider.createException(errorMessage, message, processingException);
} catch (Exception otherExceptions) {
throw DPSExceptionProvider.createException(errorMessage, "Other exception", otherExceptions);
} finally {
closeResponse(response);
}
}
Aggregations