Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error message in case of schema mismatch #789

Conversation

myazinn
Copy link
Contributor

@myazinn myazinn commented Aug 21, 2023

currently if you try to decode a case class with field not present in schema, you'll get you a bit useless message like this

[info]   java.lang.NullPointerException: Cannot invoke "org.apache.avro.Schema$Field.schema()" because "field" is null
[info]   at com.sksamuel.avro4s.decoders.SchemaFieldDecoder.<init>(records.scala:64)
[info]   at com.sksamuel.avro4s.decoders.RecordDecoder.$anonfun$1(records.scala:19)
[info]   at scala.collection.ArrayOps$.map$extension(ArrayOps.scala:932)
[info]   at scala.IArray$package$IArray$.map(IArray.scala:179)
[info]   at com.sksamuel.avro4s.decoders.RecordDecoder.decode(records.scala:20)
[info]   at com.sksamuel.avro4s.AvroDataInputStream.$init$$$anonfun$1(AvroDataInputStream.scala:40)
[info]   at scala.Option.map(Option.scala:242)
[info]   at com.sksamuel.avro4s.AvroDataInputStream.<init>(AvroDataInputStream.scala:40)
[info]   at com.sksamuel.avro4s.AvroInputStreamBuilderWithSource.build(AvroInputStream.scala:68)
[info]   at com.sksamuel.avro4s.examples.Examples.f$proxy1$1(Examples.scala:31)

With this MR, the error message should be self-contained to allow finding missing field

[info] - should read back the objects  *** FAILED ***
[info]   com.sksamuel.avro4s.Avro4sDecodingException: Field "caloriess" not found in schema {"type":"record","name":"Pizza","namespace":"com.sksamuel.avro4s.examples.Examples","fields":[{"name":"name","type":"string"},{"name":"ingredients","type":{"type":"array","items":{"type":"record","name":"Ingredient","fields":[{"name":"name","type":"string"},{"name":"sugar","type":"double"},{"name":"fat","type":"double"}]}}},{"name":"vegetarian","type":"boolean"},{"name":"vegan","type":"boolean"},{"name":"calories","type":"int"}]}
[info]   at com.sksamuel.avro4s.decoders.SchemaFieldDecoder.$init$$$anonfun$3(records.scala:65)
[info]   at scala.Option.getOrElse(Option.scala:201)
[info]   at com.sksamuel.avro4s.decoders.SchemaFieldDecoder.<init>(records.scala:65)
[info]   at com.sksamuel.avro4s.decoders.RecordDecoder.$anonfun$1(records.scala:19)
[info]   at scala.collection.ArrayOps$.map$extension(ArrayOps.scala:932)
[info]   at scala.IArray$package$IArray$.map(IArray.scala:179)
[info]   at com.sksamuel.avro4s.decoders.RecordDecoder.decode(records.scala:20)
[info]   at com.sksamuel.avro4s.AvroDataInputStream.$init$$$anonfun$1(AvroDataInputStream.scala:40)
[info]   at scala.Option.map(Option.scala:242)
[info]   at com.sksamuel.avro4s.AvroDataInputStream.<init>(AvroDataInputStream.scala:40)

@sksamuel sksamuel merged commit e6305e3 into sksamuel:master Aug 29, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants