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);
}
}
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));
}
}
}
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());
}
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());
}
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());
}
Aggregations