Search in sources :

Example 1 with PersistenceRollbackExceptionMapper

use of io.crnk.jpa.internal.PersistenceRollbackExceptionMapper in project crnk-framework by crnk-project.

the class JpaExceptionMapperTests method testPersistenceRollbackException.

@Test
public void testPersistenceRollbackException() {
    javax.persistence.RollbackException exception = new javax.persistence.RollbackException(new BadRequestException("test"));
    ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
    PersistenceRollbackExceptionMapper mapper = (PersistenceRollbackExceptionMapper) exceptionMapperRegistry.findMapperFor(javax.persistence.RollbackException.class).get();
    ErrorResponse response = mapper.toErrorResponse(exception);
    ErrorData errorData = response.getErrors().iterator().next();
    Assert.assertEquals(Integer.toString(HttpStatus.BAD_REQUEST_400), errorData.getStatus());
    Assert.assertEquals("test", errorData.getDetail());
}
Also used : BadRequestException(io.crnk.core.exception.BadRequestException) PersistenceRollbackExceptionMapper(io.crnk.jpa.internal.PersistenceRollbackExceptionMapper) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Example 2 with PersistenceRollbackExceptionMapper

use of io.crnk.jpa.internal.PersistenceRollbackExceptionMapper in project crnk-framework by crnk-project.

the class JpaModule method setupModule.

@Override
public void setupModule(ModuleContext context) {
    this.context = context;
    Set<Class> jpaTypes = new HashSet<>();
    if (config != null) {
        for (JpaRepositoryConfig<?> config : config.getRepositories()) {
            jpaTypes.add(config.getEntityClass());
        }
    }
    jpaMetaProvider = new JpaMetaProvider(jpaTypes);
    jpaMetaLookup = new MetaLookup();
    jpaMetaLookup.addProvider(jpaMetaProvider);
    jpaMetaLookup.setModuleContext(context);
    jpaMetaLookup.initialize();
    if (config != null) {
        initQueryFactory();
    }
    context.addResourceInformationBuilder(getResourceInformationProvider(context.getPropertiesProvider()));
    context.addExceptionMapper(new OptimisticLockExceptionMapper());
    context.addExceptionMapper(new PersistenceExceptionMapper(context));
    context.addExceptionMapper(new PersistenceRollbackExceptionMapper(context));
    addHibernateConstraintViolationExceptionMapper();
    addTransactionRollbackExceptionMapper();
    context.addRepositoryDecoratorFactory(new JpaRepositoryDecoratorFactory());
    if (em != null) {
        metaEnricher = new JpaMetaEnricher();
        // enrich resource meta model with JPA information where incomplete
        MetaModuleExtension metaModuleExtension = new MetaModuleExtension();
        metaModuleExtension.addProvider(metaEnricher.getProvider());
        context.addExtension(metaModuleExtension);
        setupTransactionMgmt();
    }
}
Also used : MetaLookup(io.crnk.meta.MetaLookup) JpaMetaEnricher(io.crnk.jpa.meta.internal.JpaMetaEnricher) JpaMetaProvider(io.crnk.jpa.meta.JpaMetaProvider) MetaModuleExtension(io.crnk.meta.MetaModuleExtension) OptimisticLockExceptionMapper(io.crnk.jpa.internal.OptimisticLockExceptionMapper) PersistenceRollbackExceptionMapper(io.crnk.jpa.internal.PersistenceRollbackExceptionMapper) PersistenceExceptionMapper(io.crnk.jpa.internal.PersistenceExceptionMapper) HashSet(java.util.HashSet)

Aggregations

PersistenceRollbackExceptionMapper (io.crnk.jpa.internal.PersistenceRollbackExceptionMapper)2 ErrorData (io.crnk.core.engine.document.ErrorData)1 ErrorResponse (io.crnk.core.engine.error.ErrorResponse)1 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)1 BadRequestException (io.crnk.core.exception.BadRequestException)1 OptimisticLockExceptionMapper (io.crnk.jpa.internal.OptimisticLockExceptionMapper)1 PersistenceExceptionMapper (io.crnk.jpa.internal.PersistenceExceptionMapper)1 JpaMetaProvider (io.crnk.jpa.meta.JpaMetaProvider)1 JpaMetaEnricher (io.crnk.jpa.meta.internal.JpaMetaEnricher)1 MetaLookup (io.crnk.meta.MetaLookup)1 MetaModuleExtension (io.crnk.meta.MetaModuleExtension)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1