use of io.crnk.client.ClientException in project crnk-framework by crnk-project.
the class ClientStubBase method handleError.
protected RuntimeException handleError(HttpAdapterResponse response) throws IOException {
ErrorResponse errorResponse = null;
String body = response.body();
String contentType = response.getResponseHeader(HttpHeaders.HTTP_CONTENT_TYPE);
if (body.length() > 0 && contentType != null && contentType.toLowerCase().contains(HttpHeaders.JSONAPI_CONTENT_TYPE)) {
ObjectMapper objectMapper = client.getObjectMapper();
Document document = objectMapper.readValue(body, Document.class);
if (document.getErrors() != null && !document.getErrors().isEmpty()) {
errorResponse = new ErrorResponse(document.getErrors(), response.code());
}
}
if (errorResponse == null) {
errorResponse = new ErrorResponse(null, response.code());
}
ExceptionMapperRegistry exceptionMapperRegistry = client.getExceptionMapperRegistry();
Optional<ExceptionMapper<?>> mapper = (Optional) exceptionMapperRegistry.findMapperFor(errorResponse);
if (mapper.isPresent()) {
Throwable throwable = mapper.get().fromErrorResponse(errorResponse);
if (throwable instanceof RuntimeException) {
return (RuntimeException) throwable;
} else {
return new ClientException(response.code(), response.message(), throwable);
}
} else {
return new ClientException(response.code(), response.message());
}
}
use of io.crnk.client.ClientException in project crnk-framework by crnk-project.
the class ClientStubBaseTest method checkCheckedException.
@Test
public void checkCheckedException() throws IOException {
HttpAdapterResponse response = Mockito.mock(HttpAdapterResponse.class);
Mockito.when(response.body()).thenReturn("");
Mockito.when(response.code()).thenReturn(599);
RuntimeException exception = stub.handleError(response);
Assert.assertTrue(exception instanceof ClientException);
Assert.assertTrue(exception.getCause() instanceof IOException);
}
Aggregations