Search in sources :

Example 1 with ClientException

use of org.neo4j.driver.exceptions.ClientException in project neo4j by neo4j.

the class CypherShellMultiDatabaseIntegrationTest method switchingToNonExistingDatabaseShouldGiveErrorResponseFromServerInteractive.

@Test
public void switchingToNonExistingDatabaseShouldGiveErrorResponseFromServerInteractive() throws CommandException {
    shell = new CypherShell(linePrinter, new PrettyConfig(Format.PLAIN, true, 1000), true, new ShellParameterMap());
    useCommand = new Use(shell);
    shell.connect(new ConnectionConfig("bolt", "localhost", 7687, "neo4j", "neo", Encryption.DEFAULT, ABSENT_DB_NAME));
    useCommand.execute(SYSTEM_DB_NAME);
    try {
        useCommand.execute("this_database_name_does_not_exist_in_test_container");
        fail("No ClientException thrown");
    } catch (ClientException e) {
        // In interactive we do not want to switch if the database does not exist
        assertOnSystemDB();
    }
}
Also used : ShellParameterMap(org.neo4j.shell.ShellParameterMap) CypherShell(org.neo4j.shell.CypherShell) ClientException(org.neo4j.driver.exceptions.ClientException) PrettyConfig(org.neo4j.shell.prettyprint.PrettyConfig) ConnectionConfig(org.neo4j.shell.ConnectionConfig) Test(org.junit.Test)

Example 2 with ClientException

use of org.neo4j.driver.exceptions.ClientException in project neo4j by neo4j.

the class BoltStateHandlerTest method fallbackToLegacyPing.

@Test
public void fallbackToLegacyPing() throws CommandException {
    // given
    Session sessionMock = mock(Session.class);
    Result failing = mock(Result.class);
    Result other = mock(Result.class, RETURNS_DEEP_STUBS);
    when(failing.consume()).thenThrow(new ClientException("Neo.ClientError.Procedure.ProcedureNotFound", "No procedure CALL db.ping(()"));
    when(sessionMock.run("CALL db.ping()")).thenReturn(failing);
    when(sessionMock.run("RETURN 1")).thenReturn(other);
    Driver driverMock = mock(Driver.class);
    when(driverMock.session(any())).thenReturn(sessionMock);
    OfflineBoltStateHandler boltStateHandler = new OfflineBoltStateHandler(driverMock);
    // when
    boltStateHandler.connect();
    // then
    verify(sessionMock).run("RETURN 1");
}
Also used : Driver(org.neo4j.driver.Driver) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) ClientException(org.neo4j.driver.exceptions.ClientException) Session(org.neo4j.driver.Session) FakeSession(org.neo4j.shell.test.bolt.FakeSession) Result(org.neo4j.driver.Result) Test(org.junit.Test)

Example 3 with ClientException

use of org.neo4j.driver.exceptions.ClientException in project neo4j by neo4j.

the class BoltStateHandlerTest method handleErrorsOnCommit.

@Test
public void handleErrorsOnCommit() throws CommandException {
    reset(mockDriver);
    var mockSession = spy(FakeSession.class);
    var mockTx = mock(Transaction.class);
    doThrow(new ClientException("Failed to commit :(")).when(mockTx).commit();
    when(mockSession.beginTransaction()).thenReturn(mockTx);
    when(mockDriver.session(any())).thenReturn(mockSession);
    boltStateHandler.connect();
    boltStateHandler.beginTransaction();
    assertThrows(ClientException.class, boltStateHandler::commitTransaction);
    assertFalse(boltStateHandler.isTransactionOpen());
}
Also used : ClientException(org.neo4j.driver.exceptions.ClientException) Test(org.junit.Test)

Example 4 with ClientException

use of org.neo4j.driver.exceptions.ClientException in project neo4j by neo4j.

the class AnsiLogger method getFormattedMessage.

/**
 * Formatting for Bolt exceptions.
 */
@Nonnull
public String getFormattedMessage(@Nonnull final Throwable e) {
    AnsiFormattedText msg = AnsiFormattedText.s().colorRed();
    if (isDebugEnabled()) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        e.printStackTrace(ps);
        msg.append(new String(baos.toByteArray(), StandardCharsets.UTF_8));
    } else {
        if (e instanceof AnsiFormattedException) {
            msg = msg.append(((AnsiFormattedException) e).getFormattedMessage());
        } else if (e instanceof ClientException && e.getMessage() != null && e.getMessage().contains("Missing username")) {
            // Username and password was not specified
            msg = msg.append(e.getMessage()).append("\nPlease specify --username, and optionally --password, as argument(s)").append("\nor as environment variable(s), NEO4J_USERNAME, and NEO4J_PASSWORD respectively.").append("\nSee --help for more info.");
        } else {
            Throwable cause = e;
            // Get the suppressed root cause of ServiceUnavailableExceptions
            if (e instanceof ServiceUnavailableException) {
                Throwable[] suppressed = e.getSuppressed();
                for (Throwable s : suppressed) {
                    if (s instanceof DiscoveryException) {
                        cause = getRootCause(s);
                        break;
                    }
                }
            }
            if (cause.getMessage() != null) {
                msg = msg.append(cause.getMessage());
            } else {
                msg = msg.append(cause.getClass().getSimpleName());
            }
        }
    }
    return msg.formattedString();
}
Also used : PrintStream(java.io.PrintStream) AnsiFormattedException(org.neo4j.shell.exception.AnsiFormattedException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClientException(org.neo4j.driver.exceptions.ClientException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) DiscoveryException(org.neo4j.driver.exceptions.DiscoveryException) Nonnull(javax.annotation.Nonnull)

Example 5 with ClientException

use of org.neo4j.driver.exceptions.ClientException in project neo4j by neo4j.

the class CommunityEditionEndToEndTest method testWriteInReadModeShouldFail.

@Test
void testWriteInReadModeShouldFail() {
    ClientException ex = assertThrows(ClientException.class, () -> {
        try (var session = driver.session(SessionConfig.builder().withDefaultAccessMode(AccessMode.READ).build())) {
            var query = joinAsLines("CREATE (n:Test)", "RETURN n");
            session.run(query).list();
        }
    });
    assertThat(ex.getMessage()).containsIgnoringCase("Writing in read access mode not allowed");
}
Also used : ClientException(org.neo4j.driver.exceptions.ClientException) Test(org.junit.jupiter.api.Test)

Aggregations

ClientException (org.neo4j.driver.exceptions.ClientException)11 Test (org.junit.Test)6 Result (org.neo4j.driver.Result)3 Before (org.junit.Before)2 Driver (org.neo4j.driver.Driver)2 Session (org.neo4j.driver.Session)2 ServiceUnavailableException (org.neo4j.driver.exceptions.ServiceUnavailableException)2 ConnectionConfig (org.neo4j.shell.ConnectionConfig)2 ShellStatementParser (org.neo4j.shell.parser.ShellStatementParser)2 FakeDriver (org.neo4j.shell.test.bolt.FakeDriver)2 FakeSession (org.neo4j.shell.test.bolt.FakeSession)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 Nonnull (javax.annotation.Nonnull)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Test (org.junit.jupiter.api.Test)1 DiscoveryException (org.neo4j.driver.exceptions.DiscoveryException)1 SessionExpiredException (org.neo4j.driver.exceptions.SessionExpiredException)1 ResultSummary (org.neo4j.driver.summary.ResultSummary)1 CypherShell (org.neo4j.shell.CypherShell)1