Skip to content

Commit 18f93c1

Browse files
authored
feat: add a new specific exception about json data has unknown field (#1792)
* feat: add a new specific exception about json data has unknown field * . * . * .
1 parent 3eb1475 commit 18f93c1

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/Exceptions.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,23 @@ protected InflightBytesLimitExceededException(String writerId, long currentLimit
342342
currentLimit);
343343
}
344344
}
345+
/**
346+
* Input Json data has unknown field to the schema of the JsonStreamWriter. User can either turn
347+
* on IgnoreUnknownFields option on the JsonStreamWriter, or if they don't want the error to be
348+
* ignored, they should recreate the JsonStreamWriter with the updated table schema.
349+
*/
350+
public static final class JsonDataHasUnknownFieldException extends IllegalArgumentException {
351+
private final String jsonFieldName;
352+
353+
protected JsonDataHasUnknownFieldException(String jsonFieldName) {
354+
super(String.format("JSONObject has fields unknown to BigQuery: %s.", jsonFieldName));
355+
this.jsonFieldName = jsonFieldName;
356+
}
357+
358+
public String getFieldName() {
359+
return jsonFieldName;
360+
}
361+
}
345362

346363
private Exceptions() {}
347364
}

google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ private static DynamicMessage convertJsonToProtoMessageImpl(
201201
String currentScope = jsonScope + "." + jsonName;
202202
FieldDescriptor field = protoSchema.findFieldByName(jsonLowercaseName);
203203
if (field == null && !ignoreUnknownFields) {
204-
throw new IllegalArgumentException(
205-
String.format("JSONObject has fields unknown to BigQuery: %s.", currentScope));
204+
throw new Exceptions.JsonDataHasUnknownFieldException(currentScope);
206205
} else if (field == null) {
207206
continue;
208207
}

google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,8 +1122,9 @@ public void testAllowUnknownFieldsError() throws Exception {
11221122
DynamicMessage protoMsg =
11231123
JsonToProtoMessage.convertJsonToProtoMessage(RepeatedInt64.getDescriptor(), json);
11241124
Assert.fail("Should fail");
1125-
} catch (IllegalArgumentException e) {
1125+
} catch (Exceptions.JsonDataHasUnknownFieldException e) {
11261126
assertEquals("JSONObject has fields unknown to BigQuery: root.string.", e.getMessage());
1127+
assertEquals("root.string", e.getFieldName());
11271128
}
11281129
}
11291130

0 commit comments

Comments
 (0)