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

mgrigorov pushed a commit to branch 
issue-225-better-error-msg-for-unresolved-union
in repository https://gitbox.apache.org/repos/asf/avro-rs.git

commit 52b1bff95aa4cb2c4a586e6d5e7ae441a1617aec
Author: Martin Tzvetanov Grigorov <[email protected]>
AuthorDate: Thu Jul 17 09:58:14 2025 +0300

    fix: Improve the error message for unresolved union variant
    
    Fixes #225
    
    Add the UnionSchema and the actual Value to the error message for easier
    debugging
    
    Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
---
 avro/src/error.rs | 6 +++---
 avro/src/types.rs | 5 ++++-
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/avro/src/error.rs b/avro/src/error.rs
index e68495c..e28fa9d 100644
--- a/avro/src/error.rs
+++ b/avro/src/error.rs
@@ -16,7 +16,7 @@
 // under the License.
 
 use crate::{
-    schema::{Name, Schema, SchemaKind},
+    schema::{Name, Schema, SchemaKind, UnionSchema},
     types::{Value, ValueKind},
 };
 use std::{error::Error as _, fmt};
@@ -223,8 +223,8 @@ pub enum Error {
     #[error("Key {0} not found in decimal metadata JSON")]
     GetDecimalMetadataFromJson(&'static str),
 
-    #[error("Could not find matching type in union")]
-    FindUnionVariant,
+    #[error("Could not find matching type in {schema:?} for {value:?}")]
+    FindUnionVariant { schema: UnionSchema, value: Value },
 
     #[error("Union type should not be empty")]
     EmptyUnion,
diff --git a/avro/src/types.rs b/avro/src/types.rs
index f241661..2c5bdb0 100644
--- a/avro/src/types.rs
+++ b/avro/src/types.rs
@@ -1045,7 +1045,10 @@ impl Value {
         };
         let (i, inner) = schema
             .find_schema_with_known_schemata(&v, Some(names), 
enclosing_namespace)
-            .ok_or(Error::FindUnionVariant)?;
+            .ok_or(Error::FindUnionVariant {
+                schema: schema.clone(),
+                value: v.clone(),
+            })?;
 
         Ok(Value::Union(
             i as u32,

Reply via email to