Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,12 @@ public void writeRecord(Row row) throws IOException {
// case java.sql.Types.STRUC
}
} catch (ClassCastException e) {
throw new RuntimeException(
"Field index: " + index + ", field value: " + row.getField(index) + " " + e.getMessage(), e);
// enrich the exception with detailed information.
String errorMessage = String.format(
"%s, field index: %s, field value: %s.", e.getMessage(), index, row.getField(index));
ClassCastException enrichedException = new ClassCastException(errorMessage);
enrichedException.setStackTrace(e.getStackTrace());
throw enrichedException;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ public void testWithParallelism() throws Exception {
runTest(true);
}

@Test
public void testEnrichedClassCastException() throws Exception {
exception.expect(ClassCastException.class);
exception.expectMessage(
"java.lang.String cannot be cast to java.lang.Double, field index: 3, field value: 11.11.");

JDBCOutputFormat jdbcOutputFormat = JDBCOutputFormat.buildJDBCOutputFormat()
.setDrivername(JDBCTestBase.DRIVER_CLASS)
.setDBUrl(JDBCTestBase.DB_URL)
.setQuery("insert into newbooks (id, title, author, price, qty) values (?,?,?,?,?)")
.setSqlTypes(new int[]{Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.DOUBLE, Types.INTEGER})
.finish();

jdbcOutputFormat.open(1, 1);
Row inputRow = Row.of(1001, "Java public for dummies", "Tan Ah Teck", "11.11", 11);
jdbcOutputFormat.writeRecord(inputRow);
jdbcOutputFormat.close();
}

private void runTest(boolean exploitParallelism) throws Exception {
ExecutionEnvironment environment = ExecutionEnvironment.getExecutionEnvironment();
JDBCInputFormatBuilder inputBuilder = JDBCInputFormat.buildJDBCInputFormat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.ExpectedException;

import java.io.OutputStream;
import java.sql.Connection;
Expand All @@ -35,6 +37,9 @@
*/
public class JDBCTestBase {

@Rule
public ExpectedException exception = ExpectedException.none();

public static final String DRIVER_CLASS = "org.apache.derby.jdbc.EmbeddedDriver";
public static final String DB_URL = "jdbc:derby:memory:ebookshop";
public static final String INPUT_TABLE = "books";
Expand Down