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);
}
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());
}
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));
}
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;
}
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);
}
Aggregations