Search in sources :

Example 1 with ServiceUnavailableException

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

the class MainIntegrationTest method switchingToUnavailableDatabaseIfInteractive.

@Test
public void switchingToUnavailableDatabaseIfInteractive() throws Exception {
    shell.setCommandHelper(new CommandHelper(mock(Logger.class), Historian.empty, shell));
    inputBuffer.put(String.format("neo4j%nneo%n").getBytes());
    assertEquals("", connectionConfig.username());
    assertEquals("", connectionConfig.password());
    // when
    main.connectMaybeInteractively(shell, connectionConfig, true, true, true);
    // Multiple databases are only available from 4.0
    assumeTrue(majorVersion(shell.getServerVersion()) >= 4);
    // then
    // should be connected
    assertTrue(shell.isConnected());
    // should have prompted and set the username and password
    String expectedLoginOutput = format("username: neo4j%npassword: ***%n");
    assertEquals(expectedLoginOutput, baos.toString());
    assertEquals("neo4j", connectionConfig.username());
    assertEquals("neo", connectionConfig.password());
    // Stop the default database
    shell.execute(":use " + SYSTEM_DB_NAME);
    shell.execute("STOP DATABASE " + DatabaseManager.DEFAULT_DEFAULT_DB_NAME);
    try {
        // Should get exception that database is unavailable when trying to connect
        shell.execute(":use " + DatabaseManager.DEFAULT_DEFAULT_DB_NAME);
        fail("No exception thrown");
    } catch (TransientException | ServiceUnavailableException e) {
        expectDatabaseUnavailable(e, "neo4j");
    } finally {
        // Start the default database again
        ensureDefaultDatabaseStarted();
    }
}
Also used : TransientException(org.neo4j.driver.exceptions.TransientException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) CommandHelper(org.neo4j.shell.commands.CommandHelper) Test(org.junit.Test)

Example 2 with ServiceUnavailableException

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

the class MainIntegrationTest method doesNotStartWhenDefaultDatabaseUnavailableIfInteractive.

@Test
public void doesNotStartWhenDefaultDatabaseUnavailableIfInteractive() throws Exception {
    shell.setCommandHelper(new CommandHelper(mock(Logger.class), Historian.empty, shell));
    inputBuffer.put(String.format("neo4j%nneo%n").getBytes());
    assertEquals("", connectionConfig.username());
    assertEquals("", connectionConfig.password());
    // when
    main.connectMaybeInteractively(shell, connectionConfig, true, true, true);
    // Multiple databases are only available from 4.0
    assumeTrue(majorVersion(shell.getServerVersion()) >= 4);
    // then
    // should be connected
    assertTrue(shell.isConnected());
    // should have prompted and set the username and password
    String expectedLoginOutput = format("username: neo4j%npassword: ***%n");
    assertEquals(expectedLoginOutput, baos.toString());
    assertEquals("neo4j", connectionConfig.username());
    assertEquals("neo", connectionConfig.password());
    // Stop the default database
    shell.execute(":use " + SYSTEM_DB_NAME);
    shell.execute("STOP DATABASE " + DatabaseManager.DEFAULT_DEFAULT_DB_NAME);
    try {
        shell.disconnect();
        // Should get exception that database is unavailable when trying to connect
        main.connectMaybeInteractively(shell, connectionConfig, true, true, true);
        fail("No exception thrown");
    } catch (TransientException | ServiceUnavailableException e) {
        expectDatabaseUnavailable(e, "neo4j");
    } finally {
        // Start the default database again
        ensureDefaultDatabaseStarted();
    }
}
Also used : TransientException(org.neo4j.driver.exceptions.TransientException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) CommandHelper(org.neo4j.shell.commands.CommandHelper) Test(org.junit.Test)

Example 3 with ServiceUnavailableException

use of org.neo4j.driver.exceptions.ServiceUnavailableException 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 4 with ServiceUnavailableException

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

the class CypherShell method getErrorCode.

private static String getErrorCode(Neo4jException e) {
    Neo4jException statusException = e;
    // If we encountered a later suppressed Neo4jException we use that as the basis for the status instead
    Throwable[] suppressed = e.getSuppressed();
    for (Throwable s : suppressed) {
        if (s instanceof Neo4jException) {
            statusException = (Neo4jException) s;
            break;
        }
    }
    if (statusException instanceof ServiceUnavailableException || statusException instanceof DiscoveryException) {
        // Treat this the same way as a DatabaseUnavailable error for now.
        return DATABASE_UNAVAILABLE_ERROR_CODE;
    }
    return statusException.code();
}
Also used : Neo4jException(org.neo4j.driver.exceptions.Neo4jException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) DiscoveryException(org.neo4j.driver.exceptions.DiscoveryException)

Example 5 with ServiceUnavailableException

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

the class BoltStateHandler method connect.

@Override
public ConnectionConfig connect(@Nonnull ConnectionConfig connectionConfig, ThrowingAction<CommandException> command) throws CommandException {
    if (isConnected()) {
        throw new CommandException("Already connected");
    }
    final AuthToken authToken = AuthTokens.basic(connectionConfig.username(), connectionConfig.password());
    try {
        String previousDatabaseName = activeDatabaseNameAsSetByUser;
        try {
            activeDatabaseNameAsSetByUser = connectionConfig.database();
            driver = getDriver(connectionConfig, authToken);
            reconnect(activeDatabaseNameAsSetByUser, previousDatabaseName, command);
        } catch (ServiceUnavailableException | SessionExpiredException e) {
            String scheme = connectionConfig.scheme();
            String fallbackScheme;
            switch(scheme) {
                case Scheme.NEO4J_URI_SCHEME:
                    fallbackScheme = Scheme.BOLT_URI_SCHEME;
                    break;
                case Scheme.NEO4J_LOW_TRUST_URI_SCHEME:
                    fallbackScheme = Scheme.BOLT_LOW_TRUST_URI_SCHEME;
                    break;
                case Scheme.NEO4J_HIGH_TRUST_URI_SCHEME:
                    fallbackScheme = Scheme.BOLT_HIGH_TRUST_URI_SCHEME;
                    break;
                default:
                    throw e;
            }
            connectionConfig = new ConnectionConfig(fallbackScheme, connectionConfig.host(), connectionConfig.port(), connectionConfig.username(), connectionConfig.password(), connectionConfig.encryption(), connectionConfig.database());
            try {
                driver = getDriver(connectionConfig, authToken);
                reconnect(activeDatabaseNameAsSetByUser, previousDatabaseName, command);
            } catch (Throwable fallbackThrowable) {
                // Throw the original exception to not cause confusion.
                throw e;
            }
        }
    } catch (Throwable t) {
        try {
            silentDisconnect();
        } catch (Exception e) {
            t.addSuppressed(e);
        }
        throw t;
    }
    return connectionConfig;
}
Also used : AuthToken(org.neo4j.driver.AuthToken) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) CommandException(org.neo4j.shell.exception.CommandException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ConnectionConfig(org.neo4j.shell.ConnectionConfig) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) Versions.isPasswordChangeRequiredException(org.neo4j.shell.util.Versions.isPasswordChangeRequiredException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) ClientException(org.neo4j.driver.exceptions.ClientException) Neo4jException(org.neo4j.driver.exceptions.Neo4jException) CommandException(org.neo4j.shell.exception.CommandException)

Aggregations

ServiceUnavailableException (org.neo4j.driver.exceptions.ServiceUnavailableException)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)4 Test (org.junit.Test)4 TransientException (org.neo4j.driver.exceptions.TransientException)3 CommandHelper (org.neo4j.shell.commands.CommandHelper)3 ClientException (org.neo4j.driver.exceptions.ClientException)2 DiscoveryException (org.neo4j.driver.exceptions.DiscoveryException)2 Neo4jException (org.neo4j.driver.exceptions.Neo4jException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 Nonnull (javax.annotation.Nonnull)1 AuthToken (org.neo4j.driver.AuthToken)1 SessionExpiredException (org.neo4j.driver.exceptions.SessionExpiredException)1 ConnectionConfig (org.neo4j.shell.ConnectionConfig)1 AnsiFormattedException (org.neo4j.shell.exception.AnsiFormattedException)1 CommandException (org.neo4j.shell.exception.CommandException)1 Versions.isPasswordChangeRequiredException (org.neo4j.shell.util.Versions.isPasswordChangeRequiredException)1