Skip to main content

rust_api/
error.rs

1//! Error types for rust-api framework
2
3use thiserror::Error;
4
5/// Result type alias for rust-api operations
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Main error type for the rust-api framework
9#[derive(Error, Debug)]
10pub enum Error {
11    /// Service not found in the DI container
12    #[error("Service not found: {0}")]
13    ServiceNotFound(String),
14
15    /// Service registration error
16    #[error("Service registration failed: {0}")]
17    RegistrationError(String),
18
19    /// Async service factory failed during container population
20    #[error("Async service initialization failed: {0}")]
21    ContainerError(String),
22
23    /// HTTP server error
24    #[error("HTTP server error: {0}")]
25    ServerError(String),
26
27    /// Route registration error
28    #[error("Route registration failed: {0}")]
29    RouteError(String),
30
31    /// Generic error
32    #[error("Error: {0}")]
33    Other(String),
34}
35
36impl Error {
37    /// Create a ServiceNotFound error
38    pub fn service_not_found(service_name: impl Into<String>) -> Self {
39        Self::ServiceNotFound(service_name.into())
40    }
41
42    /// Create a RegistrationError
43    pub fn registration_error(msg: impl Into<String>) -> Self {
44        Self::RegistrationError(msg.into())
45    }
46
47    /// Create a ContainerError (async factory failure)
48    pub fn container_error(msg: impl Into<String>) -> Self {
49        Self::ContainerError(msg.into())
50    }
51
52    /// Create a ServerError
53    pub fn server_error(msg: impl Into<String>) -> Self {
54        Self::ServerError(msg.into())
55    }
56
57    /// Create a RouteError
58    pub fn route_error(msg: impl Into<String>) -> Self {
59        Self::RouteError(msg.into())
60    }
61
62    /// Create an Other error
63    pub fn other(msg: impl Into<String>) -> Self {
64        Self::Other(msg.into())
65    }
66}