You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Yang <te...@gmail.com> on 2011/09/07 22:17:17 UTC

explicit schema verification?

when we declare some fields in a record, it's assumed to be mandatory unless
we specify it with a union {original_type, null}.

this feature is actually very useful to me: I declare a schema, and if some
new user creates a record, but fails to populate some mandatory field, I
want some mechanism to throw an exception.

currently I get this feature "by accident": if the user misses some
mandatory fields, when I send this record as a param to some RPC method, or
when I serialize it through SpecificDatumWriter, Avro throws a
NULLPointerException.  I can catch this and let user know that it's due to
lack of mandatory fields.

but instead of NPE, could we make Avro throw a more explicit exception, so
it's more descriptive?

Thanks
Yang

Re: explicit schema verification?

Posted by Doug Cutting <cu...@apache.org>.
On 09/07/2011 01:40 PM, James Baldassari wrote:
> We could perhaps have a new exception type that would be a subclass of
> AvroRuntimeException and would be thrown specifically when a
> non-nullable field is undefined, for example NonNullableFieldException. 

I see three choices, none perfect:

 1. Use a subclass of NullPointerException in both builder and
non-builder code.  This provides consistency and back-compatibility, but
does not permit folks to catch all Avro runtime exceptions with a single
clause.

 2. Use a subclass of AvroRuntimeException in both builder and
non-builder code.  This would not be backwards compatible, but would
provide consistency and permits folks to catch all Avro runtime
exceptions with a single clause.

 3. Throw one exception from the builder code and a different exception
from the non-builder code.

My instinct is to go with (1).  Folks can always add a second catch
clause if they wish to distinguish Avro runtime exceptions from other
runtime exceptions.  I'm not sure that distinction is always meaningful
anyway, since Avro might already throw other runtime exceptions that are
not subclasses of AvroRuntimeException.

Doug

Re: explicit schema verification?

Posted by James Baldassari <jb...@gmail.com>.
Hi Yang,

If you use the new Builder interface in trunk you'll get a slightly more
helpful error message.  When using a record builder, if you invoke build()
on it and you haven't set some non-nullable field (and it doesn't have a
default value), you'll get an AvroRuntimeException with the message "Field
[field name] does not accept null values."  This feature hasn't been
included in an Avro release yet, but this might be useful to you in the
future.  Here's a quick example:

// The protocol:
protocol People {
  record Person {
    string name;
    int year_of_birth;
  }
}

// Using the Builder
Person.newBuilder().setName("James").build();  // will throw
AvroRuntimeException

That line would throw an AvroRuntimeException because the non-nullable field
year_of_birth was not specified.  The message in the exception would say
"Field year_of_birth does not accept null values."

We could perhaps have a new exception type that would be a subclass of
AvroRuntimeException and would be thrown specifically when a non-nullable
field is undefined, for example NonNullableFieldException.  That way your
code could catch only that exception type while letting other
AvroRuntimeExceptions propagate up the stack.  If you think this would be a
useful feature, go ahead and file a JIRA.  The code change would be trivial.

-James


On Wed, Sep 7, 2011 at 4:17 PM, Yang <te...@gmail.com> wrote:

> when we declare some fields in a record, it's assumed to be mandatory
> unless we specify it with a union {original_type, null}.
>
> this feature is actually very useful to me: I declare a schema, and if some
> new user creates a record, but fails to populate some mandatory field, I
> want some mechanism to throw an exception.
>
> currently I get this feature "by accident": if the user misses some
> mandatory fields, when I send this record as a param to some RPC method, or
> when I serialize it through SpecificDatumWriter, Avro throws a
> NULLPointerException.  I can catch this and let user know that it's due to
> lack of mandatory fields.
>
> but instead of NPE, could we make Avro throw a more explicit exception, so
> it's more descriptive?
>
> Thanks
> Yang
>

Re: explicit schema verification?

Posted by Yang <te...@gmail.com>.
thanks guys: https://issues.apache.org/jira/browse/AVRO-885

On Wed, Sep 7, 2011 at 1:48 PM, Doug Cutting <cu...@apache.org> wrote:

> On 09/07/2011 01:17 PM, Yang wrote:
> > but instead of NPE, could we make Avro throw a more explicit exception,
> > so it's more descriptive?
>
> This is possible.  It could be done back-compatibly by throwing a
> subclass of NullPointerException, e.g., AvroNullValueException.  The
> logic in question is in GenericDatumWriter, where Avro builds an error
> message indicating the path to the null in the data.
>
> Please file an issue in Jira for this.
>
> Doug
>

Re: explicit schema verification?

Posted by Doug Cutting <cu...@apache.org>.
On 09/07/2011 01:17 PM, Yang wrote:
> but instead of NPE, could we make Avro throw a more explicit exception,
> so it's more descriptive?

This is possible.  It could be done back-compatibly by throwing a
subclass of NullPointerException, e.g., AvroNullValueException.  The
logic in question is in GenericDatumWriter, where Avro builds an error
message indicating the path to the null in the data.

Please file an issue in Jira for this.

Doug

Re: explicit schema verification?

Posted by Yang <te...@gmail.com>.
let me try the builder , thanks guys

On Wed, Sep 7, 2011 at 1:59 PM, Scott Carey <sc...@apache.org> wrote:

> Before the SpecificRecord  Builder API, a common practice was to use
> wrapper classes and/or static helpers for your types so that users can only
> create valid objects.
>
> On 9/7/11 1:17 PM, "Yang" <te...@gmail.com> wrote:
>
> when we declare some fields in a record, it's assumed to be mandatory
> unless we specify it with a union {original_type, null}.
>
> this feature is actually very useful to me: I declare a schema, and if some
> new user creates a record, but fails to populate some mandatory field, I
> want some mechanism to throw an exception.
>
> currently I get this feature "by accident": if the user misses some
> mandatory fields, when I send this record as a param to some RPC method, or
> when I serialize it through SpecificDatumWriter, Avro throws a
> NULLPointerException.  I can catch this and let user know that it's due to
> lack of mandatory fields.
>
> but instead of NPE, could we make Avro throw a more explicit exception, so
> it's more descriptive?
>
> Thanks
> Yang
>
>

Re: explicit schema verification?

Posted by Scott Carey <sc...@apache.org>.
Before the SpecificRecord  Builder API, a common practice was to use wrapper
classes and/or static helpers for your types so that users can only create
valid objects.

On 9/7/11 1:17 PM, "Yang" <te...@gmail.com> wrote:

> when we declare some fields in a record, it's assumed to be mandatory unless
> we specify it with a union {original_type, null}.
> 
> this feature is actually very useful to me: I declare a schema, and if some
> new user creates a record, but fails to populate some mandatory field, I want
> some mechanism to throw an exception.
> 
> currently I get this feature "by accident": if the user misses some mandatory
> fields, when I send this record as a param to some RPC method, or when I
> serialize it through SpecificDatumWriter, Avro throws a NULLPointerException.
> I can catch this and let user know that it's due to lack of mandatory fields.
> 
> but instead of NPE, could we make Avro throw a more explicit exception, so
> it's more descriptive?
> 
> Thanks
> Yang