Search in sources :

Example 26 with NoSuchRecordException

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"));
}
Also used : ResultCursor(org.neo4j.driver.async.ResultCursor) AsyncTransaction(org.neo4j.driver.async.AsyncTransaction) NoSuchRecordException(org.neo4j.driver.exceptions.NoSuchRecordException) Test(org.junit.jupiter.api.Test)

Example 27 with NoSuchRecordException

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"));
}
Also used : ResultCursor(org.neo4j.driver.async.ResultCursor) NoSuchRecordException(org.neo4j.driver.exceptions.NoSuchRecordException) Test(org.junit.jupiter.api.Test)

Example 28 with NoSuchRecordException

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"));
}
Also used : InternalRecord(org.neo4j.driver.internal.InternalRecord) PullAllResponseHandler(org.neo4j.driver.internal.handlers.PullAllResponseHandler) AsyncResultCursorImpl(org.neo4j.driver.internal.cursor.AsyncResultCursorImpl) InternalRecord(org.neo4j.driver.internal.InternalRecord) Record(org.neo4j.driver.Record) NoSuchRecordException(org.neo4j.driver.exceptions.NoSuchRecordException) Test(org.junit.jupiter.api.Test)

Example 29 with NoSuchRecordException

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));
}
Also used : Driver(org.neo4j.driver.Driver) NoSuchRecordException(org.neo4j.driver.exceptions.NoSuchRecordException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Path(javax.ws.rs.Path) CompletionException(java.util.concurrent.CompletionException) ThreadContext(org.eclipse.microprofile.context.ThreadContext) Inject(javax.inject.Inject) List(java.util.List) MediaType(javax.ws.rs.core.MediaType) CompletionStage(java.util.concurrent.CompletionStage) AsyncSession(org.neo4j.driver.async.AsyncSession) Consumes(javax.ws.rs.Consumes) Response(javax.ws.rs.core.Response) Map(java.util.Map) ResultCursor(org.neo4j.driver.async.ResultCursor) URI(java.net.URI) Status(javax.ws.rs.core.Response.Status) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) DELETE(javax.ws.rs.DELETE) Status(javax.ws.rs.core.Response.Status) ResultCursor(org.neo4j.driver.async.ResultCursor) CompletionException(java.util.concurrent.CompletionException) AsyncSession(org.neo4j.driver.async.AsyncSession) NoSuchRecordException(org.neo4j.driver.exceptions.NoSuchRecordException) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Aggregations

NoSuchRecordException (org.neo4j.driver.exceptions.NoSuchRecordException)29 NotFoundException (org.structr.api.NotFoundException)19 ClientException (org.neo4j.driver.exceptions.ClientException)18 DatabaseException (org.neo4j.driver.exceptions.DatabaseException)18 ServiceUnavailableException (org.neo4j.driver.exceptions.ServiceUnavailableException)18 TransientException (org.neo4j.driver.exceptions.TransientException)18 NetworkException (org.structr.api.NetworkException)18 RetryException (org.structr.api.RetryException)18 ResultCursor (org.neo4j.driver.async.ResultCursor)14 Record (org.neo4j.driver.Record)13 RxResult (org.neo4j.driver.reactive.RxResult)8 Test (org.junit.jupiter.api.Test)6 Entity (org.neo4j.driver.types.Entity)3 Node (org.neo4j.driver.types.Node)3 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 AsyncTransaction (org.neo4j.driver.async.AsyncTransaction)2 AsyncResultCursorImpl (org.neo4j.driver.internal.cursor.AsyncResultCursorImpl)2 PullAllResponseHandler (org.neo4j.driver.internal.handlers.PullAllResponseHandler)2 Relationship (org.neo4j.driver.types.Relationship)2