You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by rs...@apache.org on 2020/05/29 08:58:06 UTC

[avro] branch master updated: AVRO-2514:Making Avro serialization error message more actionable with... (#685)

This is an automated email from the ASF dual-hosted git repository.

rskraba 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 e3b06fb  AVRO-2514:Making Avro serialization error message more actionable with... (#685)
e3b06fb is described below

commit e3b06fb914abc05c0fc232e54be94883b784dff6
Author: Zezeng Wang <51...@qq.com>
AuthorDate: Fri May 29 16:57:50 2020 +0800

    AVRO-2514:Making Avro serialization error message more actionable with... (#685)
    
    * AVRO-2514:Making Avro serialization error message more actionable with field name
    
    * Update GenericDatumWriter.java
    
    Update to runtime exception
    
    * Update ReflectDatumWriter.java
    
    * AVRO-2514:Update GennericDatumWriter method
---
 .../org/apache/avro/generic/GenericDatumWriter.java  | 20 +++++++++++++++++++-
 .../org/apache/avro/reflect/ReflectDatumWriter.java  |  4 +---
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericDatumWriter.java b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericDatumWriter.java
index 8530094..77d01e9 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericDatumWriter.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericDatumWriter.java
@@ -179,13 +179,27 @@ public class GenericDatumWriter<D> implements DatumWriter<D> {
     }
   }
 
-  /** Helper method for adding a message to an NPE. */
+  /** Helper method for adding a message to an NPE . */
   protected NullPointerException npe(NullPointerException e, String s) {
     NullPointerException result = new NullPointerException(e.getMessage() + s);
     result.initCause(e.getCause() == null ? e : e.getCause());
     return result;
   }
 
+  /** Helper method for adding a message to an Class Cast Exception . */
+  protected ClassCastException addClassCastMsg(ClassCastException e, String s) {
+    ClassCastException result = new ClassCastException(e.getMessage() + s);
+    result.initCause(e.getCause() == null ? e : e.getCause());
+    return result;
+  }
+
+  /** Helper method for adding a message to an Avro Type Exception . */
+  protected AvroTypeException addAvroTypeMsg(AvroTypeException e, String s) {
+    AvroTypeException result = new AvroTypeException(e.getMessage() + s);
+    result.initCause(e.getCause() == null ? e : e.getCause());
+    return result;
+  }
+
   /**
    * Called to write a record. May be overridden for alternate record
    * representations.
@@ -211,6 +225,10 @@ public class GenericDatumWriter<D> implements DatumWriter<D> {
       throw unresolvedUnionException;
     } 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());
     }
   }
 
diff --git a/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumWriter.java b/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumWriter.java
index e4972b2..3dc53be 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumWriter.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/reflect/ReflectDatumWriter.java
@@ -157,9 +157,7 @@ public class ReflectDatumWriter<T> extends SpecificDatumWriter<T> {
     try {
       super.write(schema, datum, out);
     } catch (NullPointerException e) { // improve error message
-      NullPointerException result = new NullPointerException("in " + schema.getFullName() + " " + e.getMessage());
-      result.initCause(e.getCause() == null ? e : e.getCause());
-      throw result;
+      throw npe(e, " in " + schema.getFullName());
     }
   }