Skip to content

Commit c4469b2

Browse files
sgarg-CSSanchit Garg
authored andcommitted
Add catch block for inferSchema and add Error Code and SQLState to validateSchema
1 parent 40264b9 commit c4469b2

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

database-commons/src/main/java/io/cdap/plugin/db/sink/AbstractDBSink.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ private Schema inferSchema(Class<? extends Driver> driverClass) {
318318
String.format("SELECT * FROM %s WHERE 1 = 0", fullyQualifiedTableName), getErrorDetailsProvider())) {
319319
inferredFields.addAll(getSchemaReader().getSchemaFields(rs));
320320
}
321+
} catch (SQLException e) {
322+
LOG.error("Exception while trying to infer schema of database table {} for connection {}. [SQLState={}, " +
323+
"ErrorCode={}]", fullyQualifiedTableName, dbSinkConfig.getConnectionString(), e.getSQLState(),
324+
e.getErrorCode(), e);
321325
}
322326
} catch (IllegalAccessException | InstantiationException | SQLException e) {
323327
throw new InvalidStageException("JDBC Driver unavailable: " + dbSinkConfig.getJdbcPluginName(), e);
@@ -450,12 +454,12 @@ connectionString, connectionProperties, getErrorDetailsProvider())) {
450454
getFieldsValidator().validateFields(inputSchema, rs, collector);
451455
}
452456
} catch (SQLException e) {
453-
LOG.error("Exception while trying to validate schema of database table {} for connection {}.",
454-
fullyQualifiedTableName, connectionString, e);
457+
LOG.error("Exception while trying to validate schema of database table {} for connection {}. [SQLState={}," +
458+
" ErrorCode={}]", fullyQualifiedTableName, connectionString, e.getSQLState(), e.getErrorCode(), e);
455459
collector.addFailure(
456-
String.format("Exception while trying to validate schema of database table '%s' for connection '%s' with %s",
457-
fullyQualifiedTableName, connectionString, e.getMessage()),
458-
null).withStacktrace(e.getStackTrace());
460+
String.format("Exception while trying to validate schema of database table '%s' for connection '%s' with %s. "
461+
+ "[SQLState='%s', ErrorCode='%s']", fullyQualifiedTableName, connectionString, e.getMessage(),
462+
e.getSQLState(), e.getErrorCode()), null).withStacktrace(e.getStackTrace());
459463
}
460464
}
461465

0 commit comments

Comments
 (0)