From ea0309436b55d72f57478ed6f74bf31d000f5366 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 3 Sep 2022 11:49:49 +0200 Subject: refactor: improve DI container cast errors --- src/di_container.rs | 15 +++++++++++---- src/errors/di_container.rs | 13 +++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/di_container.rs b/src/di_container.rs index b0e5af1..df95be4 100644 --- a/src/di_container.rs +++ b/src/di_container.rs @@ -384,12 +384,18 @@ impl DIContainer match binding_providable { Providable::Transient(transient_binding) => Ok(SomePtr::Transient( transient_binding.cast::().map_err(|_| { - DIContainerError::CastFailed(type_name::()) + DIContainerError::CastFailed { + interface: type_name::(), + binding_kind: "transient", + } })?, )), Providable::Singleton(singleton_binding) => Ok(SomePtr::Singleton( singleton_binding.cast::().map_err(|_| { - DIContainerError::CastFailed(type_name::()) + DIContainerError::CastFailed { + interface: type_name::(), + binding_kind: "singleton", + } })?, )), #[cfg(feature = "factory")] @@ -401,8 +407,9 @@ impl DIContainer let default_factory = factory_binding .cast::>() - .map_err(|_| { - DIContainerError::CastFailed(type_name::()) + .map_err(|_| DIContainerError::CastFailed { + interface: type_name::(), + binding_kind: "factory", })?; Ok(SomePtr::Transient(default_factory())) diff --git a/src/errors/di_container.rs b/src/errors/di_container.rs index 82a3d55..5e53f43 100644 --- a/src/errors/di_container.rs +++ b/src/errors/di_container.rs @@ -11,8 +11,17 @@ use crate::errors::injectable::InjectableError; pub enum DIContainerError { /// 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}'")] -- cgit v1.2.3-18-g5258