This is an automated email from the ASF dual-hosted git repository. chaokunyang pushed a commit to tag v0.13.2-rc1 in repository https://gitbox.apache.org/repos/asf/fory.git
commit fc990730444fecfe3d5098b9f084cfb6fd10ef17 Author: Shawn Yang <[email protected]> AuthorDate: Mon Nov 3 15:52:45 2025 +0800 feat(rust): add typename to unregistered error message (#2881) <!-- **Thanks for contributing to Apache Fory™.** **If this is your first time opening a PR on fory, you can refer to [CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md).** Contribution Checklist - The **Apache Fory™** community has requirements on the naming of pr titles. You can also find instructions in [CONTRIBUTING.md](https://github.com/apache/fory/blob/main/CONTRIBUTING.md). - Apache Fory™ has a strong focus on performance. If the PR you submit will have an impact on performance, please benchmark it first and provide the benchmark result here. --> ## Why? <!-- Describe the purpose of this PR. --> ## What does this PR do? <!-- Describe the details of this PR. --> ## Related issues <!-- Is there any related issue? If this PR closes them you say say fix/closes: - #xxxx0 - #xxxx1 - Fixes #xxxx2 --> ## Does this PR introduce any user-facing change? <!-- If any user-facing interface changes, please [open an issue](https://github.com/apache/fory/issues/new/choose) describing the need to do so and update the document if necessary. Delete section if not applicable. --> - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark <!-- When the PR has an impact on performance (if you don't know whether the PR will have an impact on performance, you can submit the PR first, and if it will have impact on performance, the code reviewer will explain it), be sure to attach a benchmark data here. Delete section if not applicable. --> --- rust/fory-core/src/error.rs | 26 ++++++++++++++++++++++ rust/fory-core/src/resolver/type_resolver.rs | 14 ++++++++++-- rust/fory-core/src/serializer/core.rs | 7 +++--- rust/fory-derive/src/object/serializer.rs | 1 + rust/tests/tests/test_fory.rs | 33 ++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 5 deletions(-) diff --git a/rust/fory-core/src/error.rs b/rust/fory-core/src/error.rs index 7be8e9b56..0218b9047 100644 --- a/rust/fory-core/src/error.rs +++ b/rust/fory-core/src/error.rs @@ -451,6 +451,32 @@ impl Error { } err } + + /// Enhances a [`Error::TypeError`] with additional type name information. + /// + /// If the error is a `TypeError`, appends the type name to the message. + /// Otherwise, returns the error unchanged. + /// + /// # Example + /// ``` + /// use fory_core::error::Error; + /// + /// let err = Error::type_error("Type not registered"); + /// let enhanced = Error::enhance_type_error::<String>(err); + /// // Result: "Type not registered (type: alloc::string::String)" + /// ``` + #[inline(always)] + pub fn enhance_type_error<T: ?Sized + 'static>(err: Error) -> Error { + if let Error::TypeError(s) = err { + let mut msg = s.to_string(); + msg.push_str(" (type: "); + msg.push_str(std::any::type_name::<T>()); + msg.push(')'); + Error::type_error(msg) + } else { + err + } + } } /// Ensures a condition is true; otherwise returns an [`enum@Error`]. diff --git a/rust/fory-core/src/resolver/type_resolver.rs b/rust/fory-core/src/resolver/type_resolver.rs index 2278a401c..f86090967 100644 --- a/rust/fory-core/src/resolver/type_resolver.rs +++ b/rust/fory-core/src/resolver/type_resolver.rs @@ -287,7 +287,12 @@ fn build_struct_type_infos<T: StructSerializer>( let partial_info = type_resolver .partial_type_infos .get(&std::any::TypeId::of::<T>()) - .ok_or_else(|| Error::type_error("Partial type info not found for struct"))?; + .ok_or_else(|| { + Error::type_error(format!( + "Partial type info not found for struct (type: {})", + std::any::type_name::<T>() + )) + })?; // Get sorted field infos (fields are already sorted and have IDs assigned by the macro) let sorted_field_infos = T::fory_fields_info(type_resolver)?; @@ -861,7 +866,12 @@ impl TypeResolver { let partial_info = type_resolver .partial_type_infos .get(&std::any::TypeId::of::<T2>()) - .ok_or_else(|| Error::type_error("Partial type info not found for serializer"))?; + .ok_or_else(|| { + Error::type_error(format!( + "Partial type info not found for serializer (type: {})", + std::any::type_name::<T2>() + )) + })?; build_serializer_type_infos(partial_info, std::any::TypeId::of::<T2>()) } diff --git a/rust/fory-core/src/serializer/core.rs b/rust/fory-core/src/serializer/core.rs index 128119f0e..df443cec4 100644 --- a/rust/fory-core/src/serializer/core.rs +++ b/rust/fory-core/src/serializer/core.rs @@ -1126,9 +1126,10 @@ pub trait Serializer: 'static { where Self: Sized, { - Ok(type_resolver - .get_type_info(&std::any::TypeId::of::<Self>())? - .get_type_id()) + match type_resolver.get_type_info(&std::any::TypeId::of::<Self>()) { + Ok(info) => Ok(info.get_type_id()), + Err(e) => Err(Error::enhance_type_error::<Self>(e)), + } } /// **[USER IMPLEMENTATION REQUIRED]** Get the runtime type ID for this instance. diff --git a/rust/fory-derive/src/object/serializer.rs b/rust/fory-derive/src/object/serializer.rs index 4c0cece6b..ee5fb7201 100644 --- a/rust/fory-derive/src/object/serializer.rs +++ b/rust/fory-derive/src/object/serializer.rs @@ -175,6 +175,7 @@ pub fn derive_serializer(ast: &syn::DeriveInput, debug_enabled: bool) -> TokenSt #[inline(always)] fn fory_get_type_id(type_resolver: &fory_core::resolver::type_resolver::TypeResolver) -> Result<u32, fory_core::error::Error> { type_resolver.get_type_id(&std::any::TypeId::of::<Self>(), #type_idx) + .map_err(fory_core::error::Error::enhance_type_error::<Self>) } #[inline(always)] diff --git a/rust/tests/tests/test_fory.rs b/rust/tests/tests/test_fory.rs index 175694895..d8551a803 100644 --- a/rust/tests/tests/test_fory.rs +++ b/rust/tests/tests/test_fory.rs @@ -253,3 +253,36 @@ fn test_in_macro() { key_value.last_seen_event_time.naive_utc() ); } + +#[test] +fn test_unregistered_type_error_message() { + #[derive(ForyObject)] + struct Inner { + v: i32, + } + + #[derive(ForyObject)] + struct Outer { + v: i32, + inner: Inner, + } + + let mut fory = Fory::default(); + // Register only the outer type; inner type is intentionally not registered + fory.register::<Outer>(200).unwrap(); + let obj = Outer { + v: 1, + inner: Inner { v: 2 }, + }; + let err = fory + .serialize(&obj) + .expect_err("expected serialization to fail due to missing inner registration"); + let err_str = format!("{}", err); + // The error should include the concrete Rust type name of the inner type (the generic T) + let inner_name = std::any::type_name::<Inner>(); + assert!( + err_str.contains(inner_name), + "error did not contain inner type name; err='{}'", + err_str + ); +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
