use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.
the class ConstraintViolationExceptionMapper method fromErrorResponse.
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public ConstraintViolationException fromErrorResponse(ErrorResponse errorResponse) {
Set violations = new HashSet();
Iterable<ErrorData> errors = errorResponse.getErrors();
for (ErrorData error : errors) {
ConstraintViolationImpl violation = ConstraintViolationImpl.fromError(context.getResourceRegistry(), error);
violations.add(violation);
}
return new ConstraintViolationException(null, violations);
}
use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.
the class ConstraintViolationExceptionMapper method toErrorResponse.
@Override
public ErrorResponse toErrorResponse(ConstraintViolationException cve) {
LOGGER.warn("a ConstraintViolationException occured", cve);
List<ErrorData> errors = new ArrayList<>();
for (ConstraintViolation<?> violation : cve.getConstraintViolations()) {
ErrorDataBuilder builder = ErrorData.builder();
builder = builder.addMetaField(META_TYPE_KEY, META_TYPE_VALUE);
builder = builder.setStatus(String.valueOf(HttpStatus.UNPROCESSABLE_ENTITY_422));
builder = builder.setDetail(violation.getMessage());
builder = builder.setCode(toCode(violation));
if (violation.getMessageTemplate() != null) {
builder = builder.addMetaField(META_MESSAGE_TEMPLATE, violation.getMessageTemplate());
}
// depending on bulk update spec, we might also provide the leaf information in the future
if (violation.getRootBean() != null) {
ResourceRef resourceRef = resolvePath(violation);
builder = builder.addMetaField(META_RESOURCE_ID, resourceRef.getRootResourceId());
builder = builder.addMetaField(META_RESOURCE_TYPE, resourceRef.getRootResourceType());
builder = builder.setSourcePointer(resourceRef.getRootSourcePointer());
}
ErrorData error = builder.build();
errors.add(error);
}
return ErrorResponse.builder().setStatus(HttpStatus.UNPROCESSABLE_ENTITY_422).setErrorData(errors).build();
}
use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.
the class RegistryEntry method toResource.
private Object toResource(JsonApiResponse response) {
if (response.getErrors() != null && response.getErrors().iterator().hasNext()) {
List<ErrorData> errorList = new ArrayList<>();
response.getErrors().forEach(it -> errorList.add(it));
Optional<Integer> errorCode = errorList.stream().filter(it -> it.getStatus() != null).map(it -> Integer.parseInt(it.getStatus())).collect(Collectors.maxBy(Integer::compare));
ErrorResponse errorResponse = new ErrorResponse(errorList, errorCode.get());
ExceptionMapperRegistry exceptionMapperRegistry = moduleRegistry.getExceptionMapperRegistry();
ExceptionMapper<Throwable> exceptionMapper = exceptionMapperRegistry.findMapperFor(errorResponse).get();
return exceptionMapper.fromErrorResponse(errorResponse);
}
return response.getEntity();
}
use of io.crnk.core.engine.document.ErrorData 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());
}
use of io.crnk.core.engine.document.ErrorData in project crnk-framework by crnk-project.
the class JpaExceptionMapperTests method testConstraintException.
@Test
public void testConstraintException() {
ConstraintViolationException exception = new ConstraintViolationException("message", null, "constraint");
ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
HibernateConstraintViolationExceptionMapper mapper = (HibernateConstraintViolationExceptionMapper) exceptionMapperRegistry.findMapperFor(ConstraintViolationException.class).get();
ErrorResponse response = mapper.toErrorResponse(exception);
ErrorData errorData = response.getErrors().iterator().next();
Assert.assertEquals(Integer.toString(HttpStatus.UNPROCESSABLE_ENTITY_422), errorData.getStatus());
Assert.assertEquals(exception.getConstraintName(), errorData.getCode());
Assert.assertEquals(exception.getMessage(), errorData.getDetail());
Assert.assertTrue(mapper.accepts(response));
ConstraintViolationException deserializedException = mapper.fromErrorResponse(response);
Assert.assertEquals(exception.getMessage(), deserializedException.getMessage());
Assert.assertEquals(exception.getConstraintName(), deserializedException.getConstraintName());
}
Aggregations