Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.3.3
    • Fix Version/s: 1.4.0
    • Component/s: java
    • Labels:
      None

      Description

      Please provide more information in the exception that results from attempting to write a null value that the schema doesn't allow. For example, include the names of the type, the field and the enclosing record type.

      1. AVRO-615.patch
        4 kB
        Doug Cutting
      2. AVRO-615.patch
        4 kB
        Doug Cutting

        Activity

        John Kristian created issue -
        Hide
        John Kristian added a comment -

        As an experiment, I made a subclass of SpecificDatumWriter that throws exceptions like this:

        java.lang.NullPointerException: MyRecord.myField
        at ...
        Caused by: java.lang.NullPointerException: string
        at ...

        It worked, but I had to copy-n-paste GenericDatumWriter.writeRecord. I could have done it without copy-n-pasting, if GenericDatumWriter.writeRecord were split into two methods:

        protected void writeRecord(Schema schema, Object datum, Encoder out)
        throws IOException {
        for (Field field : schema.getFields())

        { writeField(schema, datum, field, out); }

        }

        protected void writeField(Schema schema, Object datum, Field field, Encoder out)
        throws IOException

        { write(field.schema(), getField(datum, field.name(), field.pos()), out); }

        Then the subclass could override two methods:

        @Override
        protected void writeField(Schema schema, Object datum, Field field, Encoder out)
        throws IOException {
        try

        { super.writeField(schema, datum, field, out); }

        catch (NullPointerException e)

        { NullPointerException e2 = new NullPointerException(schema.getName() + "." + field.name()); e2.initCause(e); throw e2; }

        }

        @Override
        protected void write(Schema schema, Object datum, Encoder out)
        throws IOException {
        switch (schema.getType())

        { case UNION: case NULL: break; default: if (datum == null) throw new NullPointerException(schema.getName()); }

        super.write(schema, datum, out);
        }

        Show
        John Kristian added a comment - As an experiment, I made a subclass of SpecificDatumWriter that throws exceptions like this: java.lang.NullPointerException: MyRecord.myField at ... Caused by: java.lang.NullPointerException: string at ... It worked, but I had to copy-n-paste GenericDatumWriter.writeRecord. I could have done it without copy-n-pasting, if GenericDatumWriter.writeRecord were split into two methods: protected void writeRecord(Schema schema, Object datum, Encoder out) throws IOException { for (Field field : schema.getFields()) { writeField(schema, datum, field, out); } } protected void writeField(Schema schema, Object datum, Field field, Encoder out) throws IOException { write(field.schema(), getField(datum, field.name(), field.pos()), out); } Then the subclass could override two methods: @Override protected void writeField(Schema schema, Object datum, Field field, Encoder out) throws IOException { try { super.writeField(schema, datum, field, out); } catch (NullPointerException e) { NullPointerException e2 = new NullPointerException(schema.getName() + "." + field.name()); e2.initCause(e); throw e2; } } @Override protected void write(Schema schema, Object datum, Encoder out) throws IOException { switch (schema.getType()) { case UNION: case NULL: break; default: if (datum == null) throw new NullPointerException(schema.getName()); } super.write(schema, datum, out); }
        Hide
        Philip Zeyliger added a comment -

        I think it would be great if we threw an AvroRuntimeException (or subclass thereof) instead of NPE, and provided as much detail as possible. I encourage you to provide a patch (with tests) to the DatumWriter.

        Show
        Philip Zeyliger added a comment - I think it would be great if we threw an AvroRuntimeException (or subclass thereof) instead of NPE, and provided as much detail as possible. I encourage you to provide a patch (with tests) to the DatumWriter.
        Hide
        Doug Cutting added a comment -

        Here's a patch with a test.

        Show
        Doug Cutting added a comment - Here's a patch with a test.
        Doug Cutting made changes -
        Field Original Value New Value
        Attachment AVRO-615.patch [ 12452211 ]
        Doug Cutting made changes -
        Status Open [ 1 ] Patch Available [ 10002 ]
        Assignee Doug Cutting [ cutting ]
        Fix Version/s 1.4.0 [ 12314789 ]
        Hide
        Doug Cutting added a comment -

        Here's a new version of the patch that preserves the original NullPointerException as the cause.

        If no one objects, I will commit this soon.

        Show
        Doug Cutting added a comment - Here's a new version of the patch that preserves the original NullPointerException as the cause. If no one objects, I will commit this soon.
        Doug Cutting made changes -
        Attachment AVRO-615.patch [ 12452307 ]
        Hide
        Doug Cutting added a comment -

        I committed this.

        Show
        Doug Cutting added a comment - I committed this.
        Doug Cutting made changes -
        Status Patch Available [ 10002 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Doug Cutting made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Doug Cutting
            Reporter:
            John Kristian
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development