diff options
author | HampusM <hampus@hampusmat.com> | 2022-08-31 21:44:10 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-08-31 21:44:10 +0200 |
commit | b64b7e739d56ae8cb8bb1f5f9204a4c4d810977b (patch) | |
tree | 6de3f8f7bed66673f6b122804d4dd54053a5e2b0 /src | |
parent | a9ff2f16812b56107604400a64a7f482d017eca1 (diff) |
refactor: improve async DI container cast errors
Diffstat (limited to 'src')
-rw-r--r-- | src/async_di_container.rs | 68 | ||||
-rw-r--r-- | src/errors/async_di_container.rs | 13 |
2 files changed, 52 insertions, 29 deletions
diff --git a/src/async_di_container.rs b/src/async_di_container.rs index ecf3a41..23eb2eb 100644 --- a/src/async_di_container.rs +++ b/src/async_di_container.rs @@ -406,48 +406,62 @@ impl AsyncDIContainer AsyncProvidable::Transient(transient_binding) => { Ok(SomeThreadsafePtr::Transient( transient_binding.cast::<Interface>().map_err(|_| { - AsyncDIContainerError::CastFailed(type_name::<Interface>()) + AsyncDIContainerError::CastFailed { + interface: type_name::<Interface>(), + binding_kind: "transient", + } })?, )) } AsyncProvidable::Singleton(singleton_binding) => { - Ok( - SomeThreadsafePtr::ThreadsafeSingleton( - singleton_binding.cast::<Interface>().map_err( - |err| match err { - CastError::NotArcCastable(_) => { - AsyncDIContainerError::InterfaceNotAsync(type_name::< - Interface, - >( - )) + Ok(SomeThreadsafePtr::ThreadsafeSingleton( + singleton_binding + .cast::<Interface>() + .map_err(|err| match err { + CastError::NotArcCastable(_) => { + AsyncDIContainerError::InterfaceNotAsync(type_name::< + Interface, + >( + )) + } + CastError::CastFailed { from: _, to: _ } => { + AsyncDIContainerError::CastFailed { + interface: type_name::<Interface>(), + binding_kind: "singleton", } - CastError::CastFailed { from: _, to: _ } => { - AsyncDIContainerError::CastFailed(type_name::< - Interface, - >( - )) - } - }, - )?, - ), - ) + } + })?, + )) } #[cfg(feature = "factory")] AsyncProvidable::Factory(factory_binding) => { match factory_binding.clone().cast::<Interface>() { Ok(factory) => Ok(SomeThreadsafePtr::ThreadsafeFactory(factory)), - Err(_err) => { + Err(first_err) => { use crate::interfaces::factory::IFactory; - let default_factory = - factory_binding - .cast::<dyn IFactory<(), Interface>>() - .map_err(|_| { - AsyncDIContainerError::CastFailed(type_name::< + if let CastError::NotArcCastable(_) = first_err { + return Err(AsyncDIContainerError::InterfaceNotAsync( + type_name::<Interface>(), + )); + } + + let default_factory = factory_binding + .cast::<dyn IFactory<(), Interface>>() + .map_err(|err| match err { + CastError::NotArcCastable(_) => { + AsyncDIContainerError::InterfaceNotAsync(type_name::< Interface, >( )) - })?; + } + CastError::CastFailed { from: _, to: _ } => { + AsyncDIContainerError::CastFailed { + interface: type_name::<Interface>(), + binding_kind: "factory", + } + } + })?; Ok(SomeThreadsafePtr::Transient(default_factory())) } diff --git a/src/errors/async_di_container.rs b/src/errors/async_di_container.rs index bdb6fa0..22d8fdd 100644 --- a/src/errors/async_di_container.rs +++ b/src/errors/async_di_container.rs @@ -15,8 +15,17 @@ use crate::errors::injectable::InjectableError; pub enum AsyncDIContainerError { /// Unable to cast a binding for a interface. - #[error("Unable to cast binding for interface '{0}'")] - CastFailed(&'static str), + #[error( + "Unable to cast binding for interface '{interface} with kind '{binding_kind}'" + )] + CastFailed + { + /// The interface. + interface: &'static str, + + /// The kind of the found binding. + binding_kind: &'static str, + }, /// Failed to resolve a binding for a interface. #[error("Failed to resolve binding for interface '{interface}'")] |