use of io.crnk.core.exception.InternalServerErrorException in project crnk-framework by crnk-project.
the class OperationsExceptionTest method testUnknownErrorTriggerInternalError.
@Test
public void testUnknownErrorTriggerInternalError() {
operationsModule.addFilter(new OperationFilter() {
@Override
public List<OperationResponse> filter(OperationFilterContext context, OperationFilterChain chain) {
throw new IllegalStateException("test");
}
});
MovieEntity movie1 = newMovie("1");
MovieEntity movie2 = newMovie("2");
OperationsCall insertCall = operationsClient.createCall();
insertCall.add(HttpMethod.POST, movie1);
insertCall.add(HttpMethod.POST, movie2);
try {
insertCall.execute();
Assert.fail();
} catch (InternalServerErrorException e) {
// ok
}
}
use of io.crnk.core.exception.InternalServerErrorException in project crnk-framework by crnk-project.
the class OperationsCall method execute.
public void execute() {
List<Operation> operations = new ArrayList<>();
for (QueuedOperation queuedOperation : queuedOperations) {
operations.add(queuedOperation.operation);
}
HttpAdapter adapter = client.getCrnk().getHttpAdapter();
ObjectMapper mapper = client.getCrnk().getObjectMapper();
try {
String operationsJson = mapper.writer().writeValueAsString(operations.toArray(new Operation[operations.size()]));
String url = client.getCrnk().getServiceUrlProvider().getUrl() + "/operations";
HttpAdapterRequest request = adapter.newRequest(url, HttpMethod.PATCH, operationsJson);
request.header(HttpHeaders.HTTP_CONTENT_TYPE, OperationsRequestProcessor.JSONPATCH_CONTENT_TYPE);
request.header(HttpHeaders.HTTP_HEADER_ACCEPT, OperationsRequestProcessor.JSONPATCH_CONTENT_TYPE);
HttpAdapterResponse response = request.execute();
int status = response.code();
if (status != 200) {
// general issue, status of individual operations is important.
throw new InternalServerErrorException("patch execution failed with status " + status);
}
String json = response.body();
responses = Arrays.asList(mapper.readValue(json, OperationResponse[].class));
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
use of io.crnk.core.exception.InternalServerErrorException in project crnk-framework by crnk-project.
the class ExceptionTest method testUnknownExceptionMapping.
@Test
public void testUnknownExceptionMapping() {
Task task = new Task();
task.setId(10001L);
task.setName("test");
try {
taskRepo.create(task);
Assert.fail();
} catch (InternalServerErrorException e) {
// ok
}
}
use of io.crnk.core.exception.InternalServerErrorException 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();
}
use of io.crnk.core.exception.InternalServerErrorException in project crnk-framework by crnk-project.
the class IncludeLookupSetter method fetchRelationFromEntity.
/**
* No lookup specified for the field. Attempt to load relationship from
* original POJOs. Throw an InternalServerErrorException if the field is an
* Iterable and null.
*/
private Set<Resource> fetchRelationFromEntity(List<Resource> sourceResources, ResourceField relationshipField, QueryAdapter queryAdapter, Map<ResourceIdentifier, Resource> resourceMap, Map<ResourceIdentifier, Object> entityMap, boolean allowLookup, boolean fetchRelatedEntity, boolean mustInclude, ResourceMappingConfig resourceMappingConfig) {
Set<Resource> loadedResources = new HashSet<>();
for (Resource sourceResource : sourceResources) {
ResourceIdentifier id = sourceResource.toIdentifier();
Object sourceEntity = entityMap.get(id);
if (sourceEntity != null && !(sourceEntity instanceof Resource)) {
Object relatedEntity = null;
if (fetchRelatedEntity) {
relatedEntity = relationshipField.getAccessor().getValue(sourceEntity);
if (!allowLookup && Iterable.class.isAssignableFrom(relationshipField.getType()) && relatedEntity == null) {
// note that single-valued relations are allowed to be null
throw new InternalServerErrorException(id + " relationship field collection '" + relationshipField.getJsonName() + "' can not be null. Either set the relationship as an empty " + Iterable.class.getCanonicalName() + " or add annotation @" + JsonApiLookupIncludeAutomatically.class.getCanonicalName());
}
}
// attempt to work with full relationship and fallback to relationshipId where possible
if (relatedEntity != null) {
List<Resource> relatedResources = setupRelation(sourceResource, relationshipField, relatedEntity, queryAdapter, resourceMap, entityMap, resourceMappingConfig);
loadedResources.addAll(relatedResources);
} else if (relationshipField.hasIdField()) {
Object relatedEntityID = relationshipField.getIdAccessor().getValue(sourceEntity);
setupRelationId(sourceResource, relationshipField, relatedEntityID);
if (fetchRelatedEntity && relatedEntityID != null && !allowLookup && mustInclude) {
throw new IllegalStateException("inconsistent relationship '" + relationshipField.getUnderlyingName() + "' for " + id + ", id " + "set to " + relatedEntityID + ", but related object is null and lookup disabled");
}
}
}
}
return loadedResources;
}
Aggregations