Search in sources :

Example 1 with InternalServerErrorException

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
    }
}
Also used : MovieEntity(io.crnk.operations.model.MovieEntity) OperationFilterContext(io.crnk.operations.server.OperationFilterContext) OperationFilter(io.crnk.operations.server.OperationFilter) OperationFilterChain(io.crnk.operations.server.OperationFilterChain) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) List(java.util.List) OperationsCall(io.crnk.operations.client.OperationsCall) Test(org.junit.Test)

Example 2 with InternalServerErrorException

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);
    }
}
Also used : HttpAdapterRequest(io.crnk.client.http.HttpAdapterRequest) HttpAdapter(io.crnk.client.http.HttpAdapter) HttpAdapterResponse(io.crnk.client.http.HttpAdapterResponse) ArrayList(java.util.ArrayList) Operation(io.crnk.operations.Operation) IOException(java.io.IOException) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) OperationResponse(io.crnk.operations.OperationResponse) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with InternalServerErrorException

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
    }
}
Also used : Task(io.crnk.test.mock.models.Task) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) Test(org.junit.Test)

Example 4 with InternalServerErrorException

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();
}
Also used : CrnkBoot(io.crnk.core.boot.CrnkBoot) ArrayList(java.util.ArrayList) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) ExceptionMapperRegistry(io.crnk.core.engine.internal.exception.ExceptionMapperRegistry) Document(io.crnk.core.engine.document.Document) JsonApiExceptionMapper(io.crnk.core.engine.error.JsonApiExceptionMapper) ErrorData(io.crnk.core.engine.document.ErrorData) ErrorResponse(io.crnk.core.engine.error.ErrorResponse)

Example 5 with InternalServerErrorException

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;
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Resource(io.crnk.core.engine.document.Resource) InternalServerErrorException(io.crnk.core.exception.InternalServerErrorException) HashSet(java.util.HashSet)

Aggregations

InternalServerErrorException (io.crnk.core.exception.InternalServerErrorException)8 Test (org.junit.Test)5 HttpAdapterResponse (io.crnk.client.http.HttpAdapterResponse)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Document (io.crnk.core.engine.document.Document)2 ErrorData (io.crnk.core.engine.document.ErrorData)2 Resource (io.crnk.core.engine.document.Resource)2 ArrayList (java.util.ArrayList)2 HttpAdapter (io.crnk.client.http.HttpAdapter)1 HttpAdapterRequest (io.crnk.client.http.HttpAdapterRequest)1 CrnkBoot (io.crnk.core.boot.CrnkBoot)1 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)1 ErrorResponse (io.crnk.core.engine.error.ErrorResponse)1 JsonApiExceptionMapper (io.crnk.core.engine.error.JsonApiExceptionMapper)1 ExceptionMapperRegistry (io.crnk.core.engine.internal.exception.ExceptionMapperRegistry)1 QuerySpec (io.crnk.core.queryspec.QuerySpec)1 Operation (io.crnk.operations.Operation)1 OperationResponse (io.crnk.operations.OperationResponse)1 OperationsCall (io.crnk.operations.client.OperationsCall)1 MovieEntity (io.crnk.operations.model.MovieEntity)1