From 18f93c124334464a951b3d3065bdf11bbda13dac Mon Sep 17 00:00:00 2001 From: Yiru Tang Date: Thu, 22 Sep 2022 12:58:24 -0700 Subject: [PATCH] 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 * . * . * . --- .../cloud/bigquery/storage/v1/Exceptions.java | 17 +++++++++++++++++ .../bigquery/storage/v1/JsonToProtoMessage.java | 3 +-- .../storage/v1/JsonToProtoMessageTest.java | 3 ++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/Exceptions.java b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/Exceptions.java index 2f861d86a5..ae68fcdd8e 100644 --- a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/Exceptions.java +++ b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/Exceptions.java @@ -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() {} } diff --git a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java index dde3b542bf..3f7a135ce6 100644 --- a/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java +++ b/google-cloud-bigquerystorage/src/main/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessage.java @@ -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; } diff --git a/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java b/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java index c24a49f858..3abb527ca2 100644 --- a/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java +++ b/google-cloud-bigquerystorage/src/test/java/com/google/cloud/bigquery/storage/v1/JsonToProtoMessageTest.java @@ -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()); } }