use of io.crnk.core.engine.error.ErrorResponse 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.error.ErrorResponse 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.error.ErrorResponse 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());
}
use of io.crnk.core.engine.error.ErrorResponse in project crnk-framework by crnk-project.
the class WebApplicationExceptionMapperTest method test.
@Test
public void test() {
WebApplicationExceptionMapper mapper = new WebApplicationExceptionMapper();
WebApplicationException exception = new WebApplicationException("hi");
ErrorResponse response = mapper.toErrorResponse(exception);
Iterable<ErrorData> errors = response.getErrors();
Iterator<ErrorData> iterator = errors.iterator();
ErrorData data = iterator.next();
Assert.assertFalse(iterator.hasNext());
Assert.assertEquals("500", data.getStatus());
Assert.assertEquals("hi", data.getCode());
Assert.assertTrue(mapper.accepts(response));
WebApplicationException fromErrorResponse = mapper.fromErrorResponse(response);
Assert.assertEquals("hi", fromErrorResponse.getMessage());
}
use of io.crnk.core.engine.error.ErrorResponse in project crnk-framework by crnk-project.
the class JsonapiExceptionMapperBridge method toResponse.
@Override
public Response toResponse(RuntimeException exception) {
CrnkBoot boot = this.feature.getBoot();
ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
Optional<JsonApiExceptionMapper> optional = exceptionMapperRegistry.findMapperFor(exception.getClass());
if (!optional.isPresent()) {
LOGGER.error("no exception mapper found", exception);
exception = new InternalServerErrorException(exception.getMessage());
optional = exceptionMapperRegistry.findMapperFor(exception.getClass());
}
JsonApiExceptionMapper exceptionMapper = optional.get();
ErrorResponse errorResponse = exceptionMapper.toErrorResponse(exception);
// use the Crnk document mapper to create a JSON API response
Document doc = new Document();
List<ErrorData> errors = new ArrayList<>();
for (ErrorData error : errorResponse.getErrors()) {
errors.add(error);
}
doc.setErrors(errors);
return Response.status(errorResponse.getHttpStatus()).entity(doc).header("Content-Type", JsonApiMediaType.APPLICATION_JSON_API).build();
}
Aggregations