Search in sources :

Example 1 with DiscoveryException

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

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

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

the class InteractiveShellRunnerTest method testSwitchToUnavailableDatabase3.

@Test
public void testSwitchToUnavailableDatabase3() throws Exception {
    // given
    String input = ":use foo;\n";
    TestInteractiveShellRunner sr = setupInteractiveTestShellRunner(input);
    // when
    when(sr.mockedBoltStateHandler.getActualDatabaseAsReportedByServer()).thenReturn("foo");
    doThrow(new DiscoveryException("Not available", null)).when(sr.mockedBoltStateHandler).setActiveDatabase("foo");
    sr.runner.runUntilEnd();
    // then
    assertThat(sr.output.toString(), containsString(format("myusername@foo%s> ", DATABASE_UNAVAILABLE_ERROR_PROMPT_TEXT)));
    assertThat(sr.error.toString(), containsString("Not available"));
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) DiscoveryException(org.neo4j.driver.exceptions.DiscoveryException) Test(org.junit.Test)

Aggregations

DiscoveryException (org.neo4j.driver.exceptions.DiscoveryException)3 ServiceUnavailableException (org.neo4j.driver.exceptions.ServiceUnavailableException)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.Test)1 ClientException (org.neo4j.driver.exceptions.ClientException)1 Neo4jException (org.neo4j.driver.exceptions.Neo4jException)1 AnsiFormattedException (org.neo4j.shell.exception.AnsiFormattedException)1