Search in sources :

Example 1 with AvaticaSqlException

use of org.apache.calcite.avatica.AvaticaSqlException in project calcite-avatica by apache.

the class AvaticaSqlExceptionTest method testGetters.

@Test
public void testGetters() {
    final String msg = "My query failed!";
    final int code = 42;
    final String sql = "SELECT foo FROM bar;";
    final String stacktrace = "My Stack Trace";
    final String server = "localhost:8765";
    AvaticaSqlException e = new AvaticaSqlException(msg, sql, code, Arrays.asList(stacktrace), server);
    assertTrue(e.getMessage().contains(msg));
    assertEquals(code, e.getErrorCode());
    assertEquals(sql, e.getSQLState());
    assertEquals(1, e.getStackTraces().size());
    assertEquals(stacktrace, e.getStackTraces().get(0));
    assertEquals(server, e.getRemoteServer());
}
Also used : AvaticaSqlException(org.apache.calcite.avatica.AvaticaSqlException) Test(org.junit.Test)

Example 2 with AvaticaSqlException

use of org.apache.calcite.avatica.AvaticaSqlException in project calcite-avatica by apache.

the class RemoteMetaTest method testExceptionPropagation.

@Test
public void testExceptionPropagation() throws Exception {
    final String sql = "SELECT * from EMP LIMIT FOOBARBAZ";
    ConnectionSpec.getDatabaseLock().lock();
    try (final AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url);
        final Statement stmt = conn.createStatement()) {
        try {
            // invalid SQL
            stmt.execute(sql);
            fail("Expected an AvaticaSqlException");
        } catch (AvaticaSqlException e) {
            assertEquals(ErrorResponse.UNKNOWN_ERROR_CODE, e.getErrorCode());
            assertEquals(ErrorResponse.UNKNOWN_SQL_STATE, e.getSQLState());
            assertTrue("Message should contain original SQL, was '" + e.getMessage() + "'", e.getMessage().contains(sql));
            assertEquals(1, e.getStackTraces().size());
            final String stacktrace = e.getStackTraces().get(0);
            final String substring = "unexpected token: FOOBARBAZ";
            assertTrue("Message should contain '" + substring + "', was '" + e.getMessage() + ",", stacktrace.contains(substring));
        }
    } finally {
        ConnectionSpec.getDatabaseLock().unlock();
    }
}
Also used : AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) PreparedStatement(java.sql.PreparedStatement) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) Statement(java.sql.Statement) AvaticaSqlException(org.apache.calcite.avatica.AvaticaSqlException) StringContains.containsString(org.hamcrest.core.StringContains.containsString) Test(org.junit.Test)

Aggregations

AvaticaSqlException (org.apache.calcite.avatica.AvaticaSqlException)2 Test (org.junit.Test)2 PreparedStatement (java.sql.PreparedStatement)1 Statement (java.sql.Statement)1 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)1 AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)1 StringContains.containsString (org.hamcrest.core.StringContains.containsString)1