Search in sources :

Example 1 with ClientException

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());
    }
}
Also used : ExceptionMapper(io.crnk.core.engine.error.ExceptionMapper) Optional(io.crnk.core.utils.Optional) ClientException(io.crnk.client.ClientException) Document(io.crnk.core.engine.document.Document) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ErrorResponse(io.crnk.core.engine.error.ErrorResponse)

Example 2 with ClientException

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);
}
Also used : HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) ClientException(io.crnk.client.ClientException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

ClientException (io.crnk.client.ClientException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 HttpAdapterResponse (io.crnk.client.http.HttpAdapterResponse)1 Document (io.crnk.core.engine.document.Document)1 ErrorResponse (io.crnk.core.engine.error.ErrorResponse)1 ExceptionMapper (io.crnk.core.engine.error.ExceptionMapper)1 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)1 Optional (io.crnk.core.utils.Optional)1 IOException (java.io.IOException)1 Test (org.junit.Test)1