Search in sources :

Example 1 with PersistenceExceptionMapper

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

the class JpaExceptionMapperTests method testPersistenceException.

@Test
public void testPersistenceException() {
    PersistenceException exception = new PersistenceException(new BadRequestException("test"));
    ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
    PersistenceExceptionMapper mapper = (PersistenceExceptionMapper) exceptionMapperRegistry.findMapperFor(PersistenceException.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 : PersistenceException(javax.persistence.PersistenceException) BadRequestException(io.crnk.core.exception.BadRequestException) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) ErrorData(io.crnk.core.engine.document.ErrorData) PersistenceExceptionMapper(io.crnk.jpa.internal.PersistenceExceptionMapper) ErrorResponse(io.crnk.core.engine.error.ErrorResponse) Test(org.junit.Test)

Example 2 with PersistenceExceptionMapper

use of io.crnk.jpa.internal.PersistenceExceptionMapper 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

PersistenceExceptionMapper (io.crnk.jpa.internal.PersistenceExceptionMapper)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 PersistenceRollbackExceptionMapper (io.crnk.jpa.internal.PersistenceRollbackExceptionMapper)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 PersistenceException (javax.persistence.PersistenceException)1 Test (org.junit.Test)1