Search in sources :

Example 6 with Optional

use of io.crnk.core.utils.Optional in project crnk-framework by crnk-project.

the class ExceptionMapperRegistryTest method shouldFindDescendantExceptionMapperFromError.

@Test
public void shouldFindDescendantExceptionMapperFromError() throws Exception {
    // two exception mapper will match (IllegalStateException and SomeIllegalStateException)
    // subtype should be choosen.
    ErrorData errorData = ErrorData.builder().setId("someId").build();
    ErrorResponse response = ErrorResponse.builder().setStatus(HttpStatus.BAD_REQUEST_400).setSingleErrorData(errorData).build();
    Optional<ExceptionMapper<?>> mapper = (Optional) exceptionMapperRegistry.findMapperFor(response);
    assertThat(mapper.isPresent()).isTrue();
    assertThat(mapper.get()).isExactlyInstanceOf(SomeIllegalStateExceptionMapper.class);
    Throwable throwable = mapper.get().fromErrorResponse(response);
    assertThat(throwable).isExactlyInstanceOf(SomeIllegalStateException.class);
}
Also used : JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) ExceptionMapper(io.crnk.core.engine.error.ExceptionMapper) Optional(io.crnk.core.utils.Optional) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Example 7 with Optional

use of io.crnk.core.utils.Optional in project crnk-framework by crnk-project.

the class ExceptionMapperRegistryTest method shouldNotFindDescendantExceptionMapperFromError.

@Test
public void shouldNotFindDescendantExceptionMapperFromError() throws Exception {
    ErrorData errorData = ErrorData.builder().setId("someOtherId").build();
    ErrorResponse response = ErrorResponse.builder().setStatus(HttpStatus.BAD_REQUEST_400).setSingleErrorData(errorData).build();
    Optional<ExceptionMapper<?>> mapper = (Optional) exceptionMapperRegistry.findMapperFor(response);
    assertThat(mapper.isPresent()).isTrue();
    assertThat(mapper.get()).isExactlyInstanceOf(IllegalStateExceptionMapper.class);
}
Also used : JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) ExceptionMapper(io.crnk.core.engine.error.ExceptionMapper) Optional(io.crnk.core.utils.Optional) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Example 8 with Optional

use of io.crnk.core.utils.Optional in project crnk-framework by crnk-project.

the class ExceptionMapperRegistryTest method shouldFindDirectExceptionMapperFromError.

@Test
public void shouldFindDirectExceptionMapperFromError() throws Exception {
    ErrorResponse response = ErrorResponse.builder().setStatus(HttpStatus.BAD_REQUEST_400).build();
    Optional<ExceptionMapper<?>> mapper = (Optional) exceptionMapperRegistry.findMapperFor(response);
    assertThat(mapper.isPresent()).isTrue();
    assertThat(mapper.get()).isExactlyInstanceOf(IllegalStateExceptionMapper.class);
    Throwable throwable = mapper.get().fromErrorResponse(response);
    assertThat(throwable).isExactlyInstanceOf(IllegalStateException.class);
}
Also used : JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) ExceptionMapper(io.crnk.core.engine.error.ExceptionMapper) Optional(io.crnk.core.utils.Optional) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Example 9 with Optional

use of io.crnk.core.utils.Optional in project crnk-framework by crnk-project.

the class ExceptionMapperRegistry method findMapperFor.

@SuppressWarnings({ "rawtypes", "unchecked" })
public <E extends Throwable> Optional<ExceptionMapper<E>> findMapperFor(ErrorResponse errorResponse) {
    int currentDepth = -1;
    ExceptionMapper closestExceptionMapper = null;
    for (ExceptionMapperType mapperType : exceptionMappers) {
        JsonApiExceptionMapper mapperObj = mapperType.getExceptionMapper();
        if (mapperObj instanceof ExceptionMapper) {
            ExceptionMapper mapper = (ExceptionMapper) mapperObj;
            boolean accepted = mapper.accepts(errorResponse);
            if (accepted) {
                // the exception with the most super types is chosen
                int tempDepth = countSuperTypes(mapperType.getExceptionClass());
                if (tempDepth > currentDepth) {
                    currentDepth = tempDepth;
                    closestExceptionMapper = mapper;
                }
            }
        }
    }
    return (Optional) Optional.ofNullable(closestExceptionMapper);
}
Also used : JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) ExceptionMapper(io.crnk.core.engine.error.ExceptionMapper) Optional(io.crnk.core.utils.Optional) JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper)

Aggregations

Optional (io.crnk.core.utils.Optional)9 ExceptionMapper (io.crnk.core.engine.error.ExceptionMapper)5 ErrorResponse (io.crnk.core.engine.error.ErrorResponse)4 JsonApiExceptionMapper (io.crnk.core.engine.error.JsonApiExceptionMapper)4 Test (org.junit.Test)3 ErrorData (io.crnk.core.engine.document.ErrorData)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ClientException (io.crnk.client.ClientException)1 Document (io.crnk.core.engine.document.Document)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 BaseController (io.crnk.core.engine.internal.dispatcher.controller.BaseController)1 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)1 PathBuilder (io.crnk.core.engine.internal.dispatcher.path.PathBuilder)1 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)1 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)1 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)1 ResourceFieldNotFoundException (io.crnk.core.exception.ResourceFieldNotFoundException)1 ModuleContext (io.crnk.core.module.Module.ModuleContext)1 BaseMetaPartition (io.crnk.meta.internal.BaseMetaPartition)1 IOException (java.io.IOException)1