use of org.neo4j.driver.exceptions.NoSuchRecordException in project neo4j-java-driver by neo4j.
the class AsyncTransactionIT method shouldFailSingleWithEmptyCursor.
@Test
void shouldFailSingleWithEmptyCursor() {
AsyncTransaction tx = await(session.beginTransactionAsync());
ResultCursor cursor = await(tx.runAsync("MATCH (n:NoSuchLabel) RETURN n"));
NoSuchRecordException e = assertThrows(NoSuchRecordException.class, () -> await(cursor.singleAsync()));
assertThat(e.getMessage(), containsString("result is empty"));
}
use of org.neo4j.driver.exceptions.NoSuchRecordException in project neo4j-java-driver by neo4j.
the class AsyncSessionIT method shouldFailSingleWithMultiRecordCursor.
@Test
void shouldFailSingleWithMultiRecordCursor() {
ResultCursor cursor = await(session.runAsync("UNWIND [1, 2, 3] AS x RETURN x"));
NoSuchRecordException e = assertThrows(NoSuchRecordException.class, () -> await(cursor.singleAsync()));
assertThat(e.getMessage(), startsWith("Expected a result with a single record"));
}
use of org.neo4j.driver.exceptions.NoSuchRecordException in project neo4j-java-driver by neo4j.
the class AsyncResultCursorImplTest method shouldFailWhenAskedForSingleRecordButResultContainsMore.
@Test
void shouldFailWhenAskedForSingleRecordButResultContainsMore() {
PullAllResponseHandler pullAllHandler = mock(PullAllResponseHandler.class);
Record record1 = new InternalRecord(asList("key1", "key2"), values(1, 1));
Record record2 = new InternalRecord(asList("key1", "key2"), values(2, 2));
when(pullAllHandler.nextAsync()).thenReturn(completedFuture(record1)).thenReturn(completedFuture(record2));
AsyncResultCursorImpl cursor = newCursor(pullAllHandler);
NoSuchRecordException e = assertThrows(NoSuchRecordException.class, () -> await(cursor.singleAsync()));
assertThat(e.getMessage(), containsString("Ensure your query returns only one record"));
}
use of org.neo4j.driver.exceptions.NoSuchRecordException in project quarkus-neo4j by quarkiverse.
the class FruitResource method getSingle.
// end::create[]
// tag::getSingle[]
@GET
@Path("/{id}")
public CompletionStage<Response> getSingle(Long id) {
AsyncSession session = driver.asyncSession();
return threadContext.withContextCapture(session.readTransactionAsync(tx -> tx.runAsync("MATCH (f:Fruit) WHERE id(f) = $id RETURN f", Map.of("id", id)).thenCompose(ResultCursor::singleAsync)).handle((record, exception) -> {
if (exception != null) {
Throwable source = exception;
if (exception instanceof CompletionException) {
source = exception.getCause();
}
Status status = Status.INTERNAL_SERVER_ERROR;
if (source instanceof NoSuchRecordException) {
status = Status.NOT_FOUND;
}
return Response.status(status).build();
} else {
return Response.ok(Fruit.from(record.get("f").asNode())).build();
}
})).thenCompose(response -> session.closeAsync().thenApply(signal -> response));
}
Aggregations