use of io.crnk.core.engine.internal.exception.ExceptionMapperRegistry 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.engine.internal.exception.ExceptionMapperRegistry 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.engine.internal.exception.ExceptionMapperRegistry in project crnk-framework by crnk-project.
the class ClientStubBase method handleError.
protected RuntimeException handleError(HttpAdapterResponse response) throws IOException {
ErrorResponse errorResponse = null;
String body = response.body();
String contentType = response.getResponseHeader(HttpHeaders.HTTP_CONTENT_TYPE);
if (body.length() > 0 && contentType != null && contentType.toLowerCase().contains(HttpHeaders.JSONAPI_CONTENT_TYPE)) {
ObjectMapper objectMapper = client.getObjectMapper();
Document document = objectMapper.readValue(body, Document.class);
if (document.getErrors() != null && !document.getErrors().isEmpty()) {
errorResponse = new ErrorResponse(document.getErrors(), response.code());
}
}
if (errorResponse == null) {
errorResponse = new ErrorResponse(null, response.code());
}
ExceptionMapperRegistry exceptionMapperRegistry = client.getExceptionMapperRegistry();
Optional<ExceptionMapper<?>> mapper = (Optional) exceptionMapperRegistry.findMapperFor(errorResponse);
if (mapper.isPresent()) {
Throwable throwable = mapper.get().fromErrorResponse(errorResponse);
if (throwable instanceof RuntimeException) {
return (RuntimeException) throwable;
} else {
return new ClientException(response.code(), response.message(), throwable);
}
} else {
return new ClientException(response.code(), response.message());
}
}
use of io.crnk.core.engine.internal.exception.ExceptionMapperRegistry 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.internal.exception.ExceptionMapperRegistry 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