Search in sources :

Example 1 with InputFormatException

use of org.neo4j.server.http.cypher.format.api.InputFormatException in project neo4j by neo4j.

the class Invocation method executeStatements.

private void executeStatements() {
    try {
        while (outputError == null) {
            memoryPool.reserveHeap(Statement.SHALLOW_SIZE);
            try {
                Statement statement = readStatement();
                if (statement == null) {
                    return;
                }
                executeStatement(statement);
            } finally {
                memoryPool.releaseHeap(Statement.SHALLOW_SIZE);
            }
        }
    } catch (InputFormatException e) {
        handleNeo4jError(Status.Request.InvalidFormat, e);
    } catch (KernelException | Neo4jException | AuthorizationViolationException | WriteOperationsNotAllowedException e) {
        handleNeo4jError(e.status(), e);
    } catch (DeadlockDetectedException e) {
        handleNeo4jError(Status.Transaction.DeadlockDetected, e);
    } catch (Exception e) {
        Throwable cause = e.getCause();
        if (cause instanceof Status.HasStatus) {
            handleNeo4jError(((Status.HasStatus) cause).status(), cause);
        } else {
            handleNeo4jError(Status.Statement.ExecutionFailed, e);
        }
    }
}
Also used : Status(org.neo4j.kernel.api.exceptions.Status) Statement(org.neo4j.server.http.cypher.format.api.Statement) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) Neo4jException(org.neo4j.exceptions.Neo4jException) InputFormatException(org.neo4j.server.http.cypher.format.api.InputFormatException) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) Neo4jException(org.neo4j.exceptions.Neo4jException) WriteOperationsNotAllowedException(org.neo4j.graphdb.WriteOperationsNotAllowedException) KernelException(org.neo4j.exceptions.KernelException) InputFormatException(org.neo4j.server.http.cypher.format.api.InputFormatException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException) AuthorizationViolationException(org.neo4j.graphdb.security.AuthorizationViolationException) InvalidSemanticsException(org.neo4j.exceptions.InvalidSemanticsException) DeadlockDetectedException(org.neo4j.kernel.DeadlockDetectedException) OutputFormatException(org.neo4j.server.http.cypher.format.api.OutputFormatException) WriteOperationsNotAllowedException(org.neo4j.graphdb.WriteOperationsNotAllowedException) QueryExecutionKernelException(org.neo4j.kernel.impl.query.QueryExecutionKernelException) KernelException(org.neo4j.exceptions.KernelException) AuthorizationViolationException(org.neo4j.graphdb.security.AuthorizationViolationException)

Example 2 with InputFormatException

use of org.neo4j.server.http.cypher.format.api.InputFormatException in project neo4j by neo4j.

the class StatementDeserializer method read.

InputStatement read() {
    switch(state) {
        case BEFORE_OUTER_ARRAY:
            if (!beginsWithCorrectTokens()) {
                return null;
            }
            state = State.IN_BODY;
        case IN_BODY:
            String statement = null;
            Map<String, Object> parameters = null;
            List<Object> resultsDataContents = null;
            boolean includeStats = false;
            JsonToken tok;
            try {
                while ((tok = parser.nextToken()) != null && tok != END_OBJECT) {
                    if (tok == END_ARRAY) {
                        // No more statements
                        state = State.FINISHED;
                        return null;
                    }
                    parser.nextValue();
                    String currentName = parser.getCurrentName();
                    switch(currentName) {
                        case "statement":
                            statement = parser.readValueAs(String.class);
                            break;
                        case "parameters":
                            parameters = readMap();
                            break;
                        case "resultDataContents":
                            resultsDataContents = readArray();
                            break;
                        case "includeStats":
                            includeStats = parser.getBooleanValue();
                            break;
                        default:
                            discardValue();
                    }
                }
                if (statement == null) {
                    throw new InputFormatException("No statement provided.");
                }
                return new InputStatement(statement, parameters == null ? NO_PARAMETERS : parameters, includeStats, ResultDataContent.fromNames(resultsDataContents));
            } catch (JsonParseException e) {
                throw new InputFormatException("Could not parse the incoming JSON", e);
            } catch (JsonMappingException e) {
                throw new InputFormatException("Could not map the incoming JSON", e);
            } catch (IOException e) {
                throw new ConnectionException("An error encountered while reading the inbound entity", e);
            }
        case FINISHED:
            return null;
        default:
            break;
    }
    return null;
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) InputFormatException(org.neo4j.server.http.cypher.format.api.InputFormatException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException)

Example 3 with InputFormatException

use of org.neo4j.server.http.cypher.format.api.InputFormatException in project neo4j by neo4j.

the class StatementDeserializer method beginsWithCorrectTokens.

private boolean beginsWithCorrectTokens() {
    List<JsonToken> expectedTokens = asList(START_OBJECT, FIELD_NAME, START_ARRAY);
    String expectedField = "statements";
    List<JsonToken> foundTokens = new ArrayList<>();
    try {
        for (int i = 0; i < expectedTokens.size(); i++) {
            JsonToken token = parser.nextToken();
            if (i == 0 && token == null) {
                return false;
            }
            if (token == FIELD_NAME && !expectedField.equals(parser.getText())) {
                throw new InputFormatException(String.format("Unable to deserialize request. " + "Expected first field to be '%s', but was '%s'.", expectedField, parser.getText()));
            }
            foundTokens.add(token);
        }
        if (!expectedTokens.equals(foundTokens)) {
            throw new InputFormatException(String.format("Unable to deserialize request. " + "Expected %s, found %s.", expectedTokens, foundTokens));
        }
    } catch (JsonParseException e) {
        throw new InputFormatException("Could not parse the incoming JSON", e);
    } catch (IOException e) {
        throw new ConnectionException("An error encountered while reading the inbound entity", e);
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) InputFormatException(org.neo4j.server.http.cypher.format.api.InputFormatException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException)

Example 4 with InputFormatException

use of org.neo4j.server.http.cypher.format.api.InputFormatException in project neo4j by neo4j.

the class InvocationTest method shouldHandleInputParsingErrorWhenReadingStatements.

@Test
void shouldHandleInputParsingErrorWhenReadingStatements() {
    // given
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    TransactionHandle handle = getTransactionHandle(executionEngine, registry);
    InputEventStream inputEventStream = mock(InputEventStream.class);
    when(inputEventStream.read()).thenThrow(new InputFormatException("Cannot parse input", new IOException("JSON ERROR")));
    Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, true);
    // when
    invocation.execute(outputEventStream);
    // then
    verify(internalTransaction).rollback();
    verify(registry).forget(1337L);
    InOrder outputOrder = inOrder(outputEventStream);
    outputOrder.verify(outputEventStream).writeFailure(Status.Request.InvalidFormat, "Cannot parse input");
    outputOrder.verify(outputEventStream).writeTransactionInfo(TransactionNotificationState.ROLLED_BACK, uriScheme.txCommitUri(1337L), -1);
    verifyNoMoreInteractions(outputEventStream);
}
Also used : InOrder(org.mockito.InOrder) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) IOException(java.io.IOException) InputFormatException(org.neo4j.server.http.cypher.format.api.InputFormatException) MemoryPool(org.neo4j.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Aggregations

InputFormatException (org.neo4j.server.http.cypher.format.api.InputFormatException)4 IOException (java.io.IOException)3 ConnectionException (org.neo4j.server.http.cypher.format.api.ConnectionException)3 JsonParseException (com.fasterxml.jackson.core.JsonParseException)2 JsonToken (com.fasterxml.jackson.core.JsonToken)2 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ArrayList (java.util.ArrayList)1 Test (org.junit.jupiter.api.Test)1 InOrder (org.mockito.InOrder)1 InvalidSemanticsException (org.neo4j.exceptions.InvalidSemanticsException)1 KernelException (org.neo4j.exceptions.KernelException)1 Neo4jException (org.neo4j.exceptions.Neo4jException)1 WriteOperationsNotAllowedException (org.neo4j.graphdb.WriteOperationsNotAllowedException)1 AuthorizationViolationException (org.neo4j.graphdb.security.AuthorizationViolationException)1 DeadlockDetectedException (org.neo4j.kernel.DeadlockDetectedException)1 Status (org.neo4j.kernel.api.exceptions.Status)1 QueryExecutionKernelException (org.neo4j.kernel.impl.query.QueryExecutionKernelException)1 MemoryPool (org.neo4j.memory.MemoryPool)1 InputEventStream (org.neo4j.server.http.cypher.format.api.InputEventStream)1 OutputFormatException (org.neo4j.server.http.cypher.format.api.OutputFormatException)1