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();
}
}
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();
}
}
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();
}
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();
}
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;
}
Aggregations