Search in sources :

Example 1 with JsonApiExceptionMapper

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);
}
Also used : CrnkBoot(io.crnk.core.boot.CrnkBoot) CdiTestExceptionMapper(io.crnk.internal.boot.cdi.model.CdiTestExceptionMapper) JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) Test(org.junit.Test)

Example 2 with JsonApiExceptionMapper

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);
}
Also used : JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper)

Example 3 with JsonApiExceptionMapper

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));
}
Also used : SomeIllegalStateExceptionMapper(io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest.SomeIllegalStateExceptionMapper) ExceptionMapperLookup(io.crnk.core.engine.internal.exception.ExceptionMapperLookup) IllegalStateExceptionMapper(io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest.IllegalStateExceptionMapper) SomeIllegalStateExceptionMapper(io.crnk.core.engine.internal.exception.ExceptionMapperRegistryTest.SomeIllegalStateExceptionMapper) JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with JsonApiExceptionMapper

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();
}
Also used : CrnkBoot(io.crnk.core.boot.CrnkBoot) ArrayList(java.util.ArrayList) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) Document(io.crnk.core.engine.document.Document) JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse)

Example 5 with JsonApiExceptionMapper

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;
}
Also used : Type(java.lang.reflect.Type) TypeLiteral(javax.enterprise.util.TypeLiteral) ArrayList(java.util.ArrayList) BeanManager(javax.enterprise.inject.spi.BeanManager) JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) Bean(javax.enterprise.inject.spi.Bean)

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