pub struct Error {
pub kind: ErrorKind,
pub message: String,
}Expand description
Error structure for SaDi dependency injection operations.
This structure combines an ErrorKind with a human-readable message
to provide comprehensive error information. When the tracing feature
is enabled, errors are automatically logged at appropriate levels.
§Examples
use sadi::{Error, ErrorKind};
let error = Error::service_not_registered("MyService", "transient");
println!("Error: {}", error);
// Output: Error: (ServiceNotRegistered) - No transient factory registered for type: MyServiceFields§
§kind: ErrorKindThe kind of error that occurred
message: StringHuman-readable error message with context
Implementations§
Source§impl Error
impl Error
Sourcepub fn new(kind: ErrorKind, message: impl Into<String>) -> Self
pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self
Create a new SaDi error with the specified kind and message.
This constructor also handles automatic logging when the tracing
feature is enabled. Warning-level errors (like duplicate registrations)
are logged as warnings, while other errors are logged as errors.
§Arguments
kind- The type of error that occurredmessage- A descriptive error message
§Examples
use sadi::{Error, ErrorKind};
let error = Error::new(
ErrorKind::ServiceNotRegistered,
"MyService not found"
);Sourcepub fn service_not_registered(type_name: &str, service_type: &str) -> Self
pub fn service_not_registered(type_name: &str, service_type: &str) -> Self
Create a service not registered error.
This is a convenience constructor for creating errors when a requested service type has no registered factory in the container.
§Arguments
type_name- The name of the type that wasn’t registeredservice_type- Either “transient” or “singleton”
§Examples
use sadi::Error;
let error = Error::service_not_registered("MyService", "transient");
println!("{}", error);
// Output: (ServiceNotRegistered) - No transient factory registered for type: MyServiceSourcepub fn type_mismatch(type_name: &str) -> Self
pub fn type_mismatch(type_name: &str) -> Self
Create a type mismatch error.
This error occurs when a factory function returns a value that cannot be cast to the expected type. This should be rare due to Rust’s type system, but can happen with type erasure issues.
§Arguments
type_name- The name of the type that had a mismatch
Sourcepub fn cached_type_mismatch(type_name: &str) -> Self
pub fn cached_type_mismatch(type_name: &str) -> Self
Create a cached type mismatch error.
This error occurs when a cached singleton instance cannot be cast to the expected type. This indicates an internal error in the container’s caching mechanism.
§Arguments
type_name- The name of the type that had a cached mismatch
Sourcepub fn factory_already_registered(type_name: &str, service_type: &str) -> Self
pub fn factory_already_registered(type_name: &str, service_type: &str) -> Self
Create a factory already registered error.
This error occurs when attempting to register a factory for a type
that already has a factory registered. Use the try_* methods to
handle this case gracefully.
§Arguments
type_name- The name of the type that was already registeredservice_type- Either “transient” or “singleton”
Sourcepub fn circular_dependency(dependency_chain: &[&str]) -> Self
pub fn circular_dependency(dependency_chain: &[&str]) -> Self
Create a circular dependency error.
This error occurs when the dependency graph contains a cycle, such as Service A depending on Service B, which depends on Service A. The error message includes the full dependency chain for debugging.
§Arguments
dependency_chain- The chain of dependencies that form the cycle
§Examples
use sadi::Error;
let chain = &["ServiceA", "ServiceB", "ServiceA"];
let error = Error::circular_dependency(chain);
println!("{}", error);
// Output: (CircularDependency) - Circular dependency detected: ServiceA -> ServiceB -> ServiceA