Skip to content

Commit

Permalink
feat: add a new specific exception about json data has unknown field (#…
Browse files Browse the repository at this point in the history
…1792)

* feat: add a new specific exception about json data has unknown field

* .

* .

* .
  • Loading branch information
yirutang authored Sep 22, 2022
1 parent 3eb1475 commit 18f93c1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,23 @@ protected InflightBytesLimitExceededException(String writerId, long currentLimit
currentLimit);
}
}
/**
* Input Json data has unknown field to the schema of the JsonStreamWriter. User can either turn
* on IgnoreUnknownFields option on the JsonStreamWriter, or if they don't want the error to be
* ignored, they should recreate the JsonStreamWriter with the updated table schema.
*/
public static final class JsonDataHasUnknownFieldException extends IllegalArgumentException {
private final String jsonFieldName;

protected JsonDataHasUnknownFieldException(String jsonFieldName) {
super(String.format("JSONObject has fields unknown to BigQuery: %s.", jsonFieldName));
this.jsonFieldName = jsonFieldName;
}

public String getFieldName() {
return jsonFieldName;
}
}

private Exceptions() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ private static DynamicMessage convertJsonToProtoMessageImpl(
String currentScope = jsonScope + "." + jsonName;
FieldDescriptor field = protoSchema.findFieldByName(jsonLowercaseName);
if (field == null && !ignoreUnknownFields) {
throw new IllegalArgumentException(
String.format("JSONObject has fields unknown to BigQuery: %s.", currentScope));
throw new Exceptions.JsonDataHasUnknownFieldException(currentScope);
} else if (field == null) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1122,8 +1122,9 @@ public void testAllowUnknownFieldsError() throws Exception {
DynamicMessage protoMsg =
JsonToProtoMessage.convertJsonToProtoMessage(RepeatedInt64.getDescriptor(), json);
Assert.fail("Should fail");
} catch (IllegalArgumentException e) {
} catch (Exceptions.JsonDataHasUnknownFieldException e) {
assertEquals("JSONObject has fields unknown to BigQuery: root.string.", e.getMessage());
assertEquals("root.string", e.getFieldName());
}
}

Expand Down

0 comments on commit 18f93c1

Please sign in to comment.