You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by dk...@apache.org on 2021/09/15 21:40:48 UTC
[avro] branch master updated: AVRO-3206 Provide more information in
the serialization error messages in SpecificDatumWriter
This is an automated email from the ASF dual-hosted git repository.
dkulp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new 6322fad AVRO-3206 Provide more information in the serialization error messages in SpecificDatumWriter
6322fad is described below
commit 6322fad07a01cba1f62a2f41b891b000832370ed
Author: Gyula Komlossi <gk...@cloudera.com>
AuthorDate: Tue Sep 14 17:30:09 2021 +0200
AVRO-3206 Provide more information in the serialization error messages in SpecificDatumWriter
---
.../apache/avro/specific/SpecificDatumWriter.java | 11 ++++++++-
.../avro/specific/TestSpecificDatumWriter.java | 28 ++++++++++++++++++++--
2 files changed, 36 insertions(+), 3 deletions(-)
diff --git a/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumWriter.java b/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumWriter.java
index 86b94c7..4611847 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumWriter.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificDatumWriter.java
@@ -19,6 +19,7 @@ package org.apache.avro.specific;
import java.io.IOException;
+import org.apache.avro.AvroTypeException;
import org.apache.avro.Conversion;
import org.apache.avro.LogicalType;
import org.apache.avro.Schema;
@@ -95,7 +96,15 @@ public class SpecificDatumWriter<T> extends GenericDatumWriter<T> {
value = convert(fieldSchema, logicalType, conversion, value);
}
- writeWithoutConversion(fieldSchema, value, out);
+ try {
+ writeWithoutConversion(fieldSchema, value, out);
+ } catch (NullPointerException e) {
+ throw npe(e, " in field '" + f.name() + "'");
+ } catch (ClassCastException cce) {
+ throw addClassCastMsg(cce, " in field '" + f.name() + "'");
+ } catch (AvroTypeException ate) {
+ throw addAvroTypeMsg(ate, " in field '" + f.name() + "'");
+ }
} else {
super.writeField(datum, f, out, state);
diff --git a/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificDatumWriter.java b/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificDatumWriter.java
index 7668a16..989fb15 100644
--- a/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificDatumWriter.java
+++ b/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificDatumWriter.java
@@ -17,8 +17,6 @@
*/
package org.apache.avro.specific;
-import static org.junit.Assert.assertEquals;
-
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -26,9 +24,13 @@ import org.apache.avro.Schema;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.io.JsonEncoder;
import org.apache.avro.test.Kind;
+import org.apache.avro.test.MD5;
import org.apache.avro.test.TestRecordWithUnion;
+import org.apache.avro.test.TestRecord;
import org.junit.Test;
+import static org.junit.Assert.*;
+
public class TestSpecificDatumWriter {
@Test
public void testResolveUnion() throws IOException {
@@ -50,4 +52,26 @@ public class TestSpecificDatumWriter {
assertEquals(expectedJson, out.toString("UTF-8"));
}
+ @Test
+ public void testIncompleteRecord() throws IOException {
+ final SpecificDatumWriter<TestRecord> writer = new SpecificDatumWriter<>();
+ Schema schema = TestRecord.SCHEMA$;
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ JsonEncoder encoder = EncoderFactory.get().jsonEncoder(schema, out);
+
+ writer.setSchema(schema);
+
+ TestRecord testRecord = new TestRecord();
+ testRecord.setKind(Kind.BAR);
+ testRecord.setHash(new MD5(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 }));
+
+ try {
+ writer.write(testRecord, encoder);
+ fail("Exception not thrown");
+ } catch (NullPointerException e) {
+ assertTrue(e.getMessage().contains("null of string in field 'name'"));
+ } finally {
+ out.close();
+ }
+ }
}