Skip to content

Commit 5215b11

Browse files
authored
fix(bqjdbc): Log exception messages - part 2 (#12907)
**Fully Updated** The following files received end-to-end instrumentation (including direct throw upgrades): - BigQueryJdbcCustomLogger.java - BigQueryJdbcRootLogger.java - BigQueryBaseArray.java - BigQueryBaseStruct.java - BigQueryParameterHandler.java - BigQueryDriver.java - DataSource.java - BigQueryJdbcUrlUtility.java - BigQueryJdbcProxyUtility.java - BigQueryDaemonPollingTask.java - BigQueryDatabaseMetaData.java - BigQueryPreparedStatement.java - BigQueryBaseResultSet.java **Partially Updated** The following files received instrumentation only across catch boundaries (direct raw throws remain untouched): - BigQueryConnection.java - BigQueryStatement.java
1 parent 436571e commit 5215b11

13 files changed

Lines changed: 158 additions & 68 deletions

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseArray.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,28 +71,36 @@ public final int getBaseType() {
7171

7272
@Override
7373
public final Object getArray(Map<String, Class<?>> map) throws SQLException {
74-
LOG.severe(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
75-
throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
74+
BigQueryJdbcSqlFeatureNotSupportedException ex =
75+
new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
76+
LOG.severe(ex, CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
77+
throw ex;
7678
}
7779

7880
@Override
7981
public final Object getArray(long index, int count, Map<String, Class<?>> map)
8082
throws SQLException {
81-
LOG.severe(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
82-
throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
83+
BigQueryJdbcSqlFeatureNotSupportedException ex =
84+
new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
85+
LOG.severe(ex, CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
86+
throw ex;
8387
}
8488

8589
@Override
8690
public final ResultSet getResultSet(Map<String, Class<?>> map) throws SQLException {
87-
LOG.severe(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
88-
throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
91+
BigQueryJdbcSqlFeatureNotSupportedException ex =
92+
new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
93+
LOG.severe(ex, CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
94+
throw ex;
8995
}
9096

9197
@Override
9298
public final ResultSet getResultSet(long index, int count, Map<String, Class<?>> map)
9399
throws SQLException {
94-
LOG.severe(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
95-
throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
100+
BigQueryJdbcSqlFeatureNotSupportedException ex =
101+
new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
102+
LOG.severe(ex, CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
103+
throw ex;
96104
}
97105

98106
protected Object getArrayInternal(int fromIndex, int toIndexExclusive) {
@@ -110,8 +118,9 @@ protected Object getArrayInternal(int fromIndex, int toIndexExclusive) {
110118
protected void ensureValid() throws IllegalStateException {
111119
LOG.finest("++enter++");
112120
if (!this.valid) {
113-
LOG.severe(INVALID_ARRAY);
114-
throw new IllegalStateException(INVALID_ARRAY);
121+
IllegalStateException ex = new IllegalStateException(INVALID_ARRAY);
122+
LOG.severe(ex, INVALID_ARRAY);
123+
throw ex;
115124
}
116125
}
117126

@@ -132,11 +141,13 @@ protected Tuple<Integer, Integer> createRange(long index, int count, int size)
132141
// jdbc array follows 1 based array indexing
133142
long normalisedFromIndex = index - 1;
134143
if (normalisedFromIndex + count > size) {
135-
LOG.severe(
136-
"The array index is out of range: %d, number of elements: %d.", index + count, size);
137-
throw new IllegalArgumentException(
138-
String.format(
139-
"The array index is out of range: %d, number of elements: %d.", index + count, size));
144+
IllegalArgumentException ex =
145+
new IllegalArgumentException(
146+
String.format(
147+
"The array index is out of range: %d, number of elements: %d.",
148+
index + count, size));
149+
LOG.severe(ex, ex.getMessage());
150+
throw ex;
140151
}
141152
long toIndex = normalisedFromIndex + count;
142153
return Tuple.of((int) normalisedFromIndex, (int) toIndex);

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseResultSet.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public void close() {
109109

110110
protected SQLException createCoercionException(
111111
int columnIndex, Class<?> targetClass, Exception cause) throws SQLException {
112+
LOG.severe(cause, "Coercion failed");
112113
checkClosed();
113114
StandardSQLTypeName type;
114115
String typeName;
@@ -123,8 +124,11 @@ protected SQLException createCoercionException(
123124
type = arrayField.getType().getStandardType();
124125
typeName = type.name();
125126
} else {
126-
throw new SQLException(
127-
"For a nested ResultSet from an Array, columnIndex must be 1 or 2.", cause);
127+
SQLException ex =
128+
new SQLException(
129+
"For a nested ResultSet from an Array, columnIndex must be 1 or 2.", cause);
130+
LOG.severe(ex, "For a nested ResultSet from an Array, columnIndex must be 1 or 2.");
131+
throw ex;
128132
}
129133
} else {
130134
Field field = this.schemaFieldList.get(columnIndex - 1);
@@ -144,18 +148,25 @@ private StandardSQLTypeName getStandardSQLTypeName(int columnIndex) throws SQLEx
144148
return StandardSQLTypeName.INT64;
145149
} else if (columnIndex == 2) {
146150
if (this.schema == null || this.schema.getFields().isEmpty()) {
147-
throw new SQLException("Schema not available for nested result set.");
151+
SQLException ex = new SQLException("Schema not available for nested result set.");
152+
LOG.severe(ex, "Schema not available for nested result set.");
153+
throw ex;
148154
}
149155
Field arrayField = this.schema.getFields().get(0);
150156
return arrayField.getType().getStandardType();
151157
} else {
152-
throw new SQLException("For a nested ResultSet from an Array, columnIndex must be 1 or 2.");
158+
SQLException ex =
159+
new SQLException("For a nested ResultSet from an Array, columnIndex must be 1 or 2.");
160+
LOG.severe(ex, "For a nested ResultSet from an Array, columnIndex must be 1 or 2.");
161+
throw ex;
153162
}
154163
} else {
155164
if (this.schemaFieldList == null
156165
|| columnIndex > this.schemaFieldList.size()
157166
|| columnIndex < 1) {
158-
throw new SQLException("Invalid column index: " + columnIndex);
167+
SQLException ex = new SQLException("Invalid column index: " + columnIndex);
168+
LOG.severe(ex, "Invalid column index: " + columnIndex);
169+
throw ex;
159170
}
160171
Field field = this.schemaFieldList.get(columnIndex - 1);
161172
return field.getType().getStandardType();

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseStruct.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,18 @@ abstract class BigQueryBaseStruct implements java.sql.Struct {
4242

4343
@Override
4444
public final String getSQLTypeName() throws SQLException {
45-
LOG.severe(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
46-
throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
45+
BigQueryJdbcSqlFeatureNotSupportedException ex =
46+
new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
47+
LOG.severe(ex, CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
48+
throw ex;
4749
}
4850

4951
@Override
5052
public final Object[] getAttributes(Map<String, Class<?>> map) throws SQLException {
51-
LOG.severe(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
52-
throw new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
53+
BigQueryJdbcSqlFeatureNotSupportedException ex =
54+
new BigQueryJdbcSqlFeatureNotSupportedException(CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
55+
LOG.severe(ex, CUSTOMER_TYPE_MAPPING_NOT_SUPPORTED);
56+
throw ex;
5357
}
5458

5559
static boolean isStruct(Field currentSchema) {

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDaemonPollingTask.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ else if (referenceQueueJsonRs != null) {
113113
reference.clear();
114114
}
115115
} else {
116-
LOG.severe("Null Reference Queue");
117-
throw new BigQueryJdbcRuntimeException("Null Reference Queue");
116+
BigQueryJdbcRuntimeException ex = new BigQueryJdbcRuntimeException("Null Reference Queue");
117+
LOG.severe(ex, "Null Reference Queue");
118+
throw ex;
118119
}
119120
} catch (InterruptedException ex) {
120121
LOG.severe(ex, "Interrupted in GC daemon task");

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDatabaseMetaData.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2639,6 +2639,7 @@ public ResultSet getPrimaryKeys(String catalog, String schema, String table) thr
26392639
String formattedSql = replaceSqlParameters(sql, catalog, schema, table);
26402640
return this.statement.executeQuery(formattedSql);
26412641
} catch (SQLException e) {
2642+
LOG.severe(e, "Error executing getPrimaryKeys");
26422643
throw new BigQueryJdbcException(e);
26432644
}
26442645
}
@@ -2654,6 +2655,7 @@ public ResultSet getImportedKeys(String catalog, String schema, String table)
26542655
String formattedSql = replaceSqlParameters(sql, catalog, schema, table);
26552656
return this.statement.executeQuery(formattedSql);
26562657
} catch (SQLException e) {
2658+
LOG.severe(e, "Error executing getImportedKeys");
26572659
throw new BigQueryJdbcException(e);
26582660
}
26592661
}
@@ -2669,6 +2671,7 @@ public ResultSet getExportedKeys(String catalog, String schema, String table)
26692671
String formattedSql = replaceSqlParameters(sql, catalog, schema, table);
26702672
return this.statement.executeQuery(formattedSql);
26712673
} catch (SQLException e) {
2674+
LOG.severe(e, "Error executing getExportedKeys");
26722675
throw new BigQueryJdbcException(e);
26732676
}
26742677
}
@@ -2698,6 +2701,7 @@ public ResultSet getCrossReference(
26982701
foreignTable);
26992702
return this.statement.executeQuery(formattedSql);
27002703
} catch (SQLException e) {
2704+
LOG.severe(e, "Error executing getCrossReference");
27012705
throw new BigQueryJdbcException(e);
27022706
}
27032707
}
@@ -5260,16 +5264,18 @@ private void loadDriverVersionProperties() {
52605264
if (input == null) {
52615265
String errorMessage =
52625266
"Could not find dependencies.properties. Driver version information is unavailable.";
5263-
LOG.severe(errorMessage);
5264-
throw new IllegalStateException(errorMessage);
5267+
IllegalStateException ex = new IllegalStateException(errorMessage);
5268+
LOG.severe(ex, errorMessage);
5269+
throw ex;
52655270
}
52665271
props.load(input);
52675272
String versionString = props.getProperty("version.jdbc");
52685273
if (versionString == null || versionString.trim().isEmpty()) {
52695274
String errorMessage =
52705275
"The property version.jdbc not found or empty in dependencies.properties.";
5271-
LOG.severe(errorMessage);
5272-
throw new IllegalStateException(errorMessage);
5276+
IllegalStateException ex = new IllegalStateException(errorMessage);
5277+
LOG.severe(ex, errorMessage);
5278+
throw ex;
52735279
}
52745280
parsedDriverVersion.compareAndSet(null, versionString.trim());
52755281
String[] parts = versionString.split("\\.");
@@ -5287,8 +5293,9 @@ private void loadDriverVersionProperties() {
52875293
"Error reading dependencies.properties. Driver version information is"
52885294
+ " unavailable. Error: "
52895295
+ e.getMessage();
5290-
LOG.severe(errorMessage);
5291-
throw new IllegalStateException(errorMessage, e);
5296+
IllegalStateException ex = new IllegalStateException(errorMessage, e);
5297+
LOG.severe(ex, errorMessage);
5298+
throw ex;
52925299
}
52935300
}
52945301
}

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDriver.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ public class BigQueryDriver implements Driver {
6464
try {
6565
register();
6666
} catch (SQLException e) {
67-
throw new ExceptionInInitializerError("Registering driver failed: " + e.getMessage());
67+
ExceptionInInitializerError ex =
68+
new ExceptionInInitializerError("Registering driver failed: " + e.getMessage());
69+
LOG.severe(ex, ex.getMessage());
70+
throw ex;
6871
}
6972
LoadBalancerRegistry.getDefaultRegistry().register(new PickFirstLoadBalancerProvider());
7073
}
@@ -95,8 +98,11 @@ public static BigQueryDriver getRegisteredDriver() throws IllegalStateException
9598
if (isRegistered()) {
9699
return registeredBigqueryJdbcDriver;
97100
}
98-
throw new IllegalStateException(
99-
"Driver is not registered (or it has not been registered using Driver.register() method)");
101+
IllegalStateException ex =
102+
new IllegalStateException(
103+
"Driver is not registered (or it has not been registered using Driver.register() method)");
104+
LOG.severe(ex, ex.getMessage());
105+
throw ex;
100106
}
101107

102108
/**
@@ -127,6 +133,7 @@ public Connection connect(String url, Properties info) throws SQLException {
127133
try {
128134
BigQueryJdbcUrlUtility.parseUrl(connectionUri);
129135
} catch (BigQueryJdbcRuntimeException e) {
136+
LOG.severe(e, "Failed to parse connection URL");
130137
throw new BigQueryJdbcException(e.getMessage(), e);
131138
}
132139

@@ -186,7 +193,9 @@ public Connection connect(String url, Properties info) throws SQLException {
186193
public boolean acceptsURL(String url) throws SQLException {
187194
LOG.finest("++enter++");
188195
if (url == null || url.isEmpty()) {
189-
throw new BigQueryJdbcException("Connection URL is null.");
196+
BigQueryJdbcException ex = new BigQueryJdbcException("Connection URL is null.");
197+
LOG.severe(ex, ex.getMessage());
198+
throw ex;
190199
}
191200
return url.startsWith("jdbc:bigquery:");
192201
}

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtility.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ static Map<String, String> parseOAuthProperties(DataSource ds, String callerClas
121121
try {
122122
authType = AuthType.fromValue(ds.getOAuthType());
123123
} catch (NumberFormatException exception) {
124+
LOG.severe(exception, OAUTH_TYPE_ERROR_MESSAGE);
124125
throw new IllegalArgumentException(OAUTH_TYPE_ERROR_MESSAGE);
125126
}
126127
oauthProperties.put(BigQueryJdbcUrlUtility.OAUTH_TYPE_PROPERTY_NAME, String.valueOf(authType));
@@ -458,6 +459,7 @@ private static GoogleCredentials getGoogleUserAccountCredentials(
458459
Matcher m = p.matcher(response);
459460

460461
if (!m.find()) {
462+
LOG.severe("Could not retrieve the code for user auth");
461463
throw new BigQueryJdbcRuntimeException("Could not retrieve the code for user auth");
462464
}
463465
code = m.group();
@@ -467,6 +469,7 @@ private static GoogleCredentials getGoogleUserAccountCredentials(
467469
socket.close();
468470
serverSocket.close();
469471
} else {
472+
LOG.severe("User auth only supported in desktop environments");
470473
throw new BigQueryJdbcRuntimeException("User auth only supported in desktop environments");
471474
}
472475

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcProxyUtility.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ static Map<String, String> parseProxyProperties(DataSource ds, String callerClas
7171
String proxyPort = ds.getProxyPort();
7272
if (proxyPort != null) {
7373
if (!Pattern.compile(validPortRegex).matcher(proxyPort).find()) {
74-
LOG.severe(
75-
"Illegal port number provided %s. Please provide a valid port number.", proxyPort);
76-
throw new IllegalArgumentException(
77-
String.format(
78-
"Illegal port number provided %s. Please provide a valid port number.", proxyPort));
74+
IllegalArgumentException ex =
75+
new IllegalArgumentException(
76+
String.format(
77+
"Illegal port number provided %s. Please provide a valid port number.",
78+
proxyPort));
79+
LOG.severe(ex, ex.getMessage());
80+
throw ex;
7981
}
8082
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME, proxyPort);
8183
}
@@ -91,25 +93,34 @@ static Map<String, String> parseProxyProperties(DataSource ds, String callerClas
9193
boolean isMissingProxyHostOrPortWhenProxySet =
9294
(proxyHost == null && proxyPort != null) || (proxyHost != null && proxyPort == null);
9395
if (isMissingProxyHostOrPortWhenProxySet) {
96+
IllegalArgumentException ex =
97+
new IllegalArgumentException(
98+
"Both ProxyHost and ProxyPort parameters need to be specified. No defaulting behavior"
99+
+ " occurs.");
94100
LOG.severe(
101+
ex,
95102
"Both ProxyHost and ProxyPort parameters need to be specified. No defaulting behavior occurs.");
96-
throw new IllegalArgumentException(
97-
"Both ProxyHost and ProxyPort parameters need to be specified. No defaulting behavior"
98-
+ " occurs.");
103+
throw ex;
99104
}
100105
boolean isMissingProxyUidOrPwdWhenAuthSet =
101106
(proxyUid == null && proxyPwd != null) || (proxyUid != null && proxyPwd == null);
102107
if (isMissingProxyUidOrPwdWhenAuthSet) {
103-
LOG.severe("Both ProxyUid and ProxyPwd parameters need to be specified for authentication.");
104-
throw new IllegalArgumentException(
105-
"Both ProxyUid and ProxyPwd parameters need to be specified for authentication.");
108+
IllegalArgumentException ex =
109+
new IllegalArgumentException(
110+
"Both ProxyUid and ProxyPwd parameters need to be specified for authentication.");
111+
LOG.severe(
112+
ex, "Both ProxyUid and ProxyPwd parameters need to be specified for authentication.");
113+
throw ex;
106114
}
107115
boolean isProxyAuthSetWithoutProxySettings = proxyUid != null && proxyHost == null;
108116
if (isProxyAuthSetWithoutProxySettings) {
117+
IllegalArgumentException ex =
118+
new IllegalArgumentException(
119+
"Proxy authentication provided via connection string with no proxy host or port set.");
109120
LOG.severe(
121+
ex,
110122
"Proxy authentication provided via connection string with no proxy host or port set.");
111-
throw new IllegalArgumentException(
112-
"Proxy authentication provided via connection string with no proxy host or port set.");
123+
throw ex;
113124
}
114125
return proxyProperties;
115126
}

0 commit comments

Comments
 (0)