Search in sources :

Example 6 with JsonApiExceptionMapper

use of io.crnk.core.engine.error.JsonApiExceptionMapper 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)

Example 7 with JsonApiExceptionMapper

use of io.crnk.core.engine.error.JsonApiExceptionMapper in project crnk-framework by crnk-project.

the class ExceptionMapperTypeTest method checkToString.

@Test
public void checkToString() throws Exception {
    JsonApiExceptionMapper mapper = Mockito.mock(JsonApiExceptionMapper.class);
    Mockito.when(mapper.toString()).thenReturn("customMapper");
    ExceptionMapperType type = new ExceptionMapperType(IllegalStateException.class, mapper);
    Assert.assertEquals("ExceptionMapperType[exceptionClass=java.lang.IllegalStateException, exceptionMapper=customMapper]", type.toString());
}
Also used : JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) Test(org.junit.Test)

Example 8 with JsonApiExceptionMapper

use of io.crnk.core.engine.error.JsonApiExceptionMapper in project crnk-framework by crnk-project.

the class CrnkBootTest method testServiceDiscovery.

@Test
public void testServiceDiscovery() {
    CrnkBoot boot = new CrnkBoot();
    boot.setServiceDiscoveryFactory(serviceDiscoveryFactory);
    boot.setServiceUrlProvider(mock(ServiceUrlProvider.class));
    Module module = mock(Module.class);
    DocumentFilter filter = mock(DocumentFilter.class);
    JsonApiExceptionMapper exceptionMapper = new TestExceptionMapper();
    Mockito.when(serviceDiscovery.getInstancesByType(eq(DocumentFilter.class))).thenReturn(Arrays.asList(filter));
    Mockito.when(serviceDiscovery.getInstancesByType(eq(Module.class))).thenReturn(Arrays.asList(module));
    Mockito.when(serviceDiscovery.getInstancesByType(eq(JsonApiExceptionMapper.class))).thenReturn(Arrays.asList(exceptionMapper));
    boot.boot();
    ModuleRegistry moduleRegistry = boot.getModuleRegistry();
    Assert.assertTrue(moduleRegistry.getModules().contains(module));
    Assert.assertTrue(moduleRegistry.getFilters().contains(filter));
    Assert.assertTrue(moduleRegistry.getExceptionMapperLookup().getExceptionMappers().contains(exceptionMapper));
}
Also used : ConstantServiceUrlProvider(io.crnk.core.engine.url.ConstantServiceUrlProvider) ServiceUrlProvider(io.crnk.core.engine.url.ServiceUrlProvider) ModuleRegistry(io.crnk.core.module.ModuleRegistry) DocumentFilter(io.crnk.core.engine.filter.DocumentFilter) SimpleModule(io.crnk.core.module.SimpleModule) Module(io.crnk.core.module.Module) JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) Test(org.junit.Test)

Example 9 with JsonApiExceptionMapper

use of io.crnk.core.engine.error.JsonApiExceptionMapper in project crnk-framework by crnk-project.

the class DefaultExceptionMapperLookup method getExceptionMappers.

@Override
public Set<JsonApiExceptionMapper> getExceptionMappers() {
    Reflections reflections;
    if (resourceSearchPackages != null) {
        reflections = new Reflections(resourceSearchPackages);
    } else {
        reflections = new Reflections();
    }
    Set<Class<?>> exceptionMapperClasses = reflections.getTypesAnnotatedWith(ExceptionMapperProvider.class);
    Set<JsonApiExceptionMapper> exceptionMappers = new HashSet<>();
    for (Class<?> exceptionMapperClazz : exceptionMapperClasses) {
        if (!JsonApiExceptionMapper.class.isAssignableFrom(exceptionMapperClazz)) {
            throw new InvalidResourceException(exceptionMapperClazz.getCanonicalName() + " is not an implementation of JsonApiExceptionMapper");
        }
        try {
            exceptionMappers.add((JsonApiExceptionMapper<? extends Throwable>) exceptionMapperClazz.newInstance());
        } catch (Exception e) {
            throw new InvalidResourceException(exceptionMapperClazz.getCanonicalName() + " can not be initialized", e);
        }
    }
    return exceptionMappers;
}
Also used : InvalidResourceException(io.crnk.core.exception.InvalidResourceException) JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) InvalidResourceException(io.crnk.core.exception.InvalidResourceException) Reflections(org.reflections.Reflections) HashSet(java.util.HashSet)

Example 10 with JsonApiExceptionMapper

use of io.crnk.core.engine.error.JsonApiExceptionMapper in project crnk-framework by crnk-project.

the class GuiceServiceDiscoveryTest method exceptionMapperDiscovery.

@Test
public void exceptionMapperDiscovery() {
    CrnkBoot boot = new CrnkBoot();
    boot.setServiceDiscovery(discovery);
    boot.boot();
    Optional<JsonApiExceptionMapper> mapper = boot.getExceptionMapperRegistry().findMapperFor(TestException.class);
    Assert.assertTrue(mapper.isPresent());
    Assert.assertTrue(mapper.get() instanceof TestExceptionMapper);
}
Also used : CrnkBoot(io.crnk.core.boot.CrnkBoot) JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) TestExceptionMapper(io.crnk.test.mock.TestExceptionMapper) Test(org.junit.Test)

Aggregations

JsonApiExceptionMapper (io.crnk.core.engine.error.JsonApiExceptionMapper)11 Test (org.junit.Test)5 CrnkBoot (io.crnk.core.boot.CrnkBoot)3 ErrorData (io.crnk.core.engine.document.ErrorData)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Document (io.crnk.core.engine.document.Document)1 ErrorResponse (io.crnk.core.engine.error.ErrorResponse)1 ExceptionMapper (io.crnk.core.engine.error.ExceptionMapper)1 DocumentFilter (io.crnk.core.engine.filter.DocumentFilter)1 ExceptionMapperLookup (io.crnk.core.engine.internal.exception.ExceptionMapperLookup)1 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)1 IllegalStateExceptionMapper (io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest.IllegalStateExceptionMapper)1 SomeIllegalStateExceptionMapper (io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest.SomeIllegalStateExceptionMapper)1 ConstantServiceUrlProvider (io.crnk.core.engine.url.ConstantServiceUrlProvider)1 ServiceUrlProvider (io.crnk.core.engine.url.ServiceUrlProvider)1 InternalServerErrorException (io.crnk.core.exception.InternalServerErrorException)1 InvalidResourceException (io.crnk.core.exception.InvalidResourceException)1 Module (io.crnk.core.module.Module)1 ModuleRegistry (io.crnk.core.module.ModuleRegistry)1