Search in sources :

Example 1 with BadRequestException

use of io.crnk.core.exception.BadRequestException in project crnk-framework by crnk-project.

the class ActivitiQuerySpecMapper method find.

public static <T> List<T> find(Query activitiQuery, QuerySpec querySpec, List<FilterSpec> baseFilters) {
    try {
        applyFilterSpec(activitiQuery, querySpec, baseFilters);
        applySortSpec(activitiQuery, querySpec);
        Long limit = querySpec.getLimit();
        if (limit != null) {
            return activitiQuery.listPage((int) querySpec.getOffset(), querySpec.getLimit().intValue());
        } else {
            PreconditionUtil.assertEquals("page offset not supported", Long.valueOf(0L), querySpec.getOffset());
            return activitiQuery.list();
        }
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        throw new BadRequestException(e.getMessage(), e);
    } catch (IllegalStateException e) {
        throw new BadRequestException(e.getMessage(), e);
    }
}
Also used : BadRequestException(io.crnk.core.exception.BadRequestException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with BadRequestException

use of io.crnk.core.exception.BadRequestException in project crnk-framework by crnk-project.

the class OffsetLimitPagingBehavior method doEnrichPageLinksInformation.

private void doEnrichPageLinksInformation(PagedLinksInformation linksInformation, Long total, Boolean isNextPageAvailable, QueryAdapter queryAdapter, boolean hasResults, PagingSpecUrlBuilder urlBuilder) {
    OffsetLimitPagingSpec offsetLimitPagingSpec = (OffsetLimitPagingSpec) queryAdapter.getPagingSpec();
    long pageSize = offsetLimitPagingSpec.getLimit();
    long offset = offsetLimitPagingSpec.getOffset();
    long currentPage = offset / pageSize;
    if (currentPage * pageSize != offset) {
        throw new BadRequestException("offset " + offset + " is not a multiple of limit " + pageSize);
    }
    if (total != null) {
        isNextPageAvailable = offset + pageSize < total;
    }
    if (offset > 0 || hasResults) {
        Long totalPages = total != null ? (total + pageSize - 1) / pageSize : null;
        QueryAdapter pageSpec = queryAdapter.duplicate();
        pageSpec.setPagingSpec(new OffsetLimitPagingSpec(0L, pageSize));
        linksInformation.setFirst(urlBuilder.build(pageSpec));
        if (totalPages != null && totalPages > 0) {
            pageSpec.setPagingSpec(new OffsetLimitPagingSpec((totalPages - 1) * pageSize, pageSize));
            linksInformation.setLast(urlBuilder.build(pageSpec));
        }
        if (currentPage > 0) {
            pageSpec.setPagingSpec(new OffsetLimitPagingSpec((currentPage - 1) * pageSize, pageSize));
            linksInformation.setPrev(urlBuilder.build(pageSpec));
        }
        if (isNextPageAvailable) {
            pageSpec.setPagingSpec(new OffsetLimitPagingSpec((currentPage + 1) * pageSize, pageSize));
            linksInformation.setNext(urlBuilder.build(pageSpec));
        }
    }
}
Also used : QueryAdapter(io.crnk.core.engine.query.QueryAdapter) BadRequestException(io.crnk.core.exception.BadRequestException)

Example 3 with BadRequestException

use of io.crnk.core.exception.BadRequestException 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 4 with BadRequestException

use of io.crnk.core.exception.BadRequestException 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 5 with BadRequestException

use of io.crnk.core.exception.BadRequestException in project crnk-framework by crnk-project.

the class JpaExceptionMapperTests method testTransactionRollbackException.

@Test
public void testTransactionRollbackException() {
    javax.transaction.RollbackException exception = new javax.transaction.RollbackException() {

        public Throwable getCause() {
            return new BadRequestException("test");
        }
    };
    ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
    TransactionRollbackExceptionMapper mapper = (TransactionRollbackExceptionMapper) exceptionMapperRegistry.findMapperFor(exception.getClass()).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 : TransactionRollbackExceptionMapper(io.crnk.jpa.internal.TransactionRollbackExceptionMapper) BadRequestException(io.crnk.core.exception.BadRequestException) 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)

Aggregations

BadRequestException (io.crnk.core.exception.BadRequestException)6 ErrorData (io.crnk.core.engine.document.ErrorData)3 ErrorResponse (io.crnk.core.engine.error.ErrorResponse)3 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)3 Test (org.junit.Test)3 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)1 FilterSpec (io.crnk.core.queryspec.FilterSpec)1 QuerySpec (io.crnk.core.queryspec.QuerySpec)1 PersistenceExceptionMapper (io.crnk.jpa.internal.PersistenceExceptionMapper)1 PersistenceRollbackExceptionMapper (io.crnk.jpa.internal.PersistenceRollbackExceptionMapper)1 TransactionRollbackExceptionMapper (io.crnk.jpa.internal.TransactionRollbackExceptionMapper)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PersistenceException (javax.persistence.PersistenceException)1