use of io.crnk.core.engine.error.JsonApiExceptionMapper in project crnk-framework by crnk-project.
the class CdiServiceDiscoveryTest method testExceptionMapper.
@Test
public void testExceptionMapper() {
CrnkBoot boot = new CrnkBoot();
boot.boot();
Optional<JsonApiExceptionMapper> mapper = boot.getExceptionMapperRegistry().findMapperFor(IllegalStateException.class);
Assert.assertTrue(mapper.isPresent());
Assert.assertTrue(mapper.get() instanceof CdiTestExceptionMapper);
}
use of io.crnk.core.engine.error.JsonApiExceptionMapper in project crnk-framework by crnk-project.
the class ExceptionMapperRegistry method findMapperFor.
public Optional<JsonApiExceptionMapper> findMapperFor(Class<? extends Throwable> exceptionClass) {
int currentDistance = Integer.MAX_VALUE;
JsonApiExceptionMapper closestExceptionMapper = null;
for (ExceptionMapperType mapperType : exceptionMappers) {
int tempDistance = getDistanceBetweenExceptions(exceptionClass, mapperType.getExceptionClass());
if (tempDistance < currentDistance) {
currentDistance = tempDistance;
closestExceptionMapper = mapperType.getExceptionMapper();
if (currentDistance == 0) {
break;
}
}
}
return Optional.ofNullable(closestExceptionMapper);
}
use of io.crnk.core.engine.error.JsonApiExceptionMapper in project crnk-framework by crnk-project.
the class ModuleRegistryTest method testExceptionMappers.
@Test
public void testExceptionMappers() {
ExceptionMapperLookup exceptionMapperLookup = moduleRegistry.getExceptionMapperLookup();
Set<JsonApiExceptionMapper> exceptionMappers = exceptionMapperLookup.getExceptionMappers();
Set<Class<?>> classes = new HashSet<>();
for (JsonApiExceptionMapper exceptionMapper : exceptionMappers) {
classes.add(exceptionMapper.getClass());
}
Assert.assertTrue(classes.contains(IllegalStateExceptionMapper.class));
Assert.assertTrue(classes.contains(SomeIllegalStateExceptionMapper.class));
}
use of io.crnk.core.engine.error.JsonApiExceptionMapper in project crnk-framework by crnk-project.
the class JsonapiExceptionMapperBridge method toResponse.
@Override
public Response toResponse(RuntimeException exception) {
CrnkBoot boot = this.feature.getBoot();
ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
Optional<JsonApiExceptionMapper> optional = exceptionMapperRegistry.findMapperFor(exception.getClass());
if (!optional.isPresent()) {
LOGGER.error("no exception mapper found", exception);
exception = new InternalServerErrorException(exception.getMessage());
optional = exceptionMapperRegistry.findMapperFor(exception.getClass());
}
JsonApiExceptionMapper exceptionMapper = optional.get();
ErrorResponse errorResponse = exceptionMapper.toErrorResponse(exception);
// use the Crnk document mapper to create a JSON API response
Document doc = new Document();
List<ErrorData> errors = new ArrayList<>();
for (ErrorData error : errorResponse.getErrors()) {
errors.add(error);
}
doc.setErrors(errors);
return Response.status(errorResponse.getHttpStatus()).entity(doc).header("Content-Type", JsonApiMediaType.APPLICATION_JSON_API).build();
}
use of io.crnk.core.engine.error.JsonApiExceptionMapper in project crnk-framework by crnk-project.
the class CdiServiceDiscovery method getInstancesByType.
@SuppressWarnings("unchecked")
@Override
public <T> List<T> getInstancesByType(Class<T> clazz) {
BeanManager beanManager = getBeanManager();
Type type = clazz;
if (clazz == JsonApiExceptionMapper.class) {
TypeLiteral<JsonApiExceptionMapper<?>> typeLiteral = new TypeLiteral<JsonApiExceptionMapper<?>>() {
};
type = typeLiteral.getType();
}
Set<Bean<?>> beans = beanManager.getBeans(type);
List<T> list = new ArrayList<>();
for (Bean<?> bean : beans) {
CreationalContext<?> creationalContext = beanManager.createCreationalContext(bean);
T object = (T) beanManager.getReference(bean, type, creationalContext);
list.add(object);
}
return list;
}
Aggregations