JDBCSSHRecordStoreConnection implements SSHRecordStoreConnection { RuntimeException sqlError(SQLException ex, String caller) { String sqlState = ex.getSQLState(); int code = ResourceException.INTERNAL_SERVER_ERROR; String msg; if (ex instanceof SQLTimeoutException) { code = ResourceException.SERVICE_UNAVAILABLE; msg = "Statement cancelled due to timeout"; } else { msg = ex.getMessage() + ", state: " + sqlState + ", code: " + ex.getErrorCode(); } LOGGER.error("SQLError: {} - {}", caller, msg); return new ResourceException(code, msg); } JDBCSSHRecordStoreConnection(Connection con); @Override void setOperationTimeout(int queryTimeout); @Override void close(); @Override SSHCertRecord getSSHCertRecord(String instanceId, String service); @Override boolean updateSSHCertRecord(SSHCertRecord sshCertRecord); @Override boolean insertSSHCertRecord(SSHCertRecord sshCertRecord); @Override boolean deleteSSHCertRecord(String instanceId, String service); @Override int deleteExpiredSSHCertRecords(int expiryTimeMins); static final String DB_COLUMN_INSTANCE_ID; static final String DB_COLUMN_SERVICE; static final String DB_COLUMN_CLIENT_IP; static final String DB_COLUMN_PRIVATE_IP; static final String DB_COLUMN_PRINCIPALS; }
@Test public void testSqlError() throws SQLException { JDBCSSHRecordStoreConnection jdbcConn = new JDBCSSHRecordStoreConnection(mockConn); SQLException ex = new SQLException("sql-reason", "08S01", 9999); ResourceException rEx = (ResourceException) jdbcConn.sqlError(ex, "sqlError"); assertEquals(ResourceException.INTERNAL_SERVER_ERROR, rEx.getCode()); ex = new SQLException("sql-reason", "40001", 9999); rEx = (ResourceException) jdbcConn.sqlError(ex, "sqlError"); assertEquals(ResourceException.INTERNAL_SERVER_ERROR, rEx.getCode()); SQLTimeoutException tex = new SQLTimeoutException(); rEx = (ResourceException) jdbcConn.sqlError(tex, "sqlError"); assertEquals(ResourceException.SERVICE_UNAVAILABLE, rEx.getCode()); jdbcConn.close(); }