Search in sources :

Example 1 with CommandException

use of org.neo4j.shell.exception.CommandException in project neo4j by neo4j.

the class CypherShellTest method setWhenOfflineShouldWork.

@Test
public void setWhenOfflineShouldWork() throws ParameterException, CommandException {
    CypherShell shell = new OfflineTestShell(logger, mockedBoltStateHandler, mockedPrettyPrinter);
    when(mockedBoltStateHandler.isConnected()).thenReturn(false);
    when(mockedBoltStateHandler.runCypher(anyString(), anyMap())).thenThrow(new CommandException("not connected"));
    Object result = shell.getParameterMap().setParameter("bob", "99");
    assertEquals(99L, result);
}
Also used : Mockito.anyObject(org.mockito.Mockito.anyObject) CommandException(org.neo4j.shell.exception.CommandException) Test(org.junit.Test)

Example 2 with CommandException

use of org.neo4j.shell.exception.CommandException in project neo4j by neo4j.

the class BoltStateHandlerTest method exceptionFromRunQueryDoesNotResetActualDatabaseNameToUnresolved.

@Test
public void exceptionFromRunQueryDoesNotResetActualDatabaseNameToUnresolved() throws CommandException {
    Session sessionMock = mock(Session.class);
    Result resultMock = mock(Result.class);
    Driver driverMock = stubResultSummaryInAnOpenSession(resultMock, sessionMock, "9.4.1-ALPHA", "my_default_db");
    ClientException databaseNotFound = new ClientException("Neo.ClientError.Database.DatabaseNotFound", "blah");
    when(sessionMock.run(any(Query.class))).thenThrow(databaseNotFound).thenReturn(resultMock);
    BoltStateHandler handler = new BoltStateHandler((s, authToken, config) -> driverMock, false);
    handler.connect(config);
    try {
        handler.runCypher("RETURN \"hello\"", Collections.emptyMap());
        fail("should fail on runCypher");
    } catch (Exception e) {
        assertThat(e, is(databaseNotFound));
        assertEquals("my_default_db", handler.getActualDatabaseAsReportedByServer());
    }
}
Also used : Driver(org.neo4j.driver.Driver) FakeDriver(org.neo4j.shell.test.bolt.FakeDriver) ClientException(org.neo4j.driver.exceptions.ClientException) SessionExpiredException(org.neo4j.driver.exceptions.SessionExpiredException) CommandException(org.neo4j.shell.exception.CommandException) ServiceUnavailableException(org.neo4j.driver.exceptions.ServiceUnavailableException) 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 CommandException

use of org.neo4j.shell.exception.CommandException 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)

Example 4 with CommandException

use of org.neo4j.shell.exception.CommandException in project neo4j by neo4j.

the class Source method execute.

@Override
public void execute(@Nonnull final String argString) throws ExitException, CommandException {
    String filename = simpleArgParse(argString, 1, 1, COMMAND_NAME, getUsage())[0];
    try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(filename)))) {
        bufferedReader.lines().forEach(line -> statementParser.parseMoreText(line + "\n"));
        List<String> statements = statementParser.consumeStatements();
        // Executing this could fail but we try anyway to avoid hiding errors
        statementParser.incompleteStatement().ifPresent(statements::add);
        for (String statement : statements) {
            cypherShell.execute(statement);
        }
    } catch (IOException e) {
        throw new CommandException(format("Cannot find file: '%s'", filename), e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) CommandException(org.neo4j.shell.exception.CommandException) FileInputStream(java.io.FileInputStream)

Example 5 with CommandException

use of org.neo4j.shell.exception.CommandException in project neo4j by neo4j.

the class CypherShellVerboseIntegrationTest method cypherWithOrder.

@Test
public void cypherWithOrder() throws CommandException {
    // given
    String serverVersion = shell.getServerVersion();
    assumeThat(version(serverVersion), greaterThanOrEqualTo(version("3.6")));
    // Make sure we are creating a new NEW index
    try {
        shell.execute("DROP INDEX ON :Person(age)");
    } catch (Exception e) {
    // ignore if the index didn't exist
    }
    shell.execute("CREATE INDEX ON :Person(age)");
    shell.execute("CALL db.awaitIndexes()");
    // when
    shell.execute("CYPHER RUNTIME=INTERPRETED EXPLAIN MATCH (n:Person) WHERE n.age >= 18 RETURN n.name, n.age ORDER BY n.age");
    // then
    String actual = linePrinter.output();
    assertThat(actual, containsString("Order"));
    assertThat(actual, containsString("n.age ASC"));
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) EvaluationException(org.neo4j.cypher.internal.evaluator.EvaluationException) ExpectedException(org.junit.rules.ExpectedException) CommandException(org.neo4j.shell.exception.CommandException) Test(org.junit.Test)

Aggregations

CommandException (org.neo4j.shell.exception.CommandException)5 Test (org.junit.Test)3 ClientException (org.neo4j.driver.exceptions.ClientException)2 ServiceUnavailableException (org.neo4j.driver.exceptions.ServiceUnavailableException)2 SessionExpiredException (org.neo4j.driver.exceptions.SessionExpiredException)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 ExpectedException (org.junit.rules.ExpectedException)1 Mockito.anyObject (org.mockito.Mockito.anyObject)1 EvaluationException (org.neo4j.cypher.internal.evaluator.EvaluationException)1 AuthToken (org.neo4j.driver.AuthToken)1 Driver (org.neo4j.driver.Driver)1 Result (org.neo4j.driver.Result)1 Session (org.neo4j.driver.Session)1 Neo4jException (org.neo4j.driver.exceptions.Neo4jException)1 ConnectionConfig (org.neo4j.shell.ConnectionConfig)1 FakeDriver (org.neo4j.shell.test.bolt.FakeDriver)1