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);
}
}
}
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;
}
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;
}
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);
}
Aggregations