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    /// HTTP server error
20    #[error("HTTP server error: {0}")]
21    ServerError(String),
22
23    /// Route registration error
24    #[error("Route registration failed: {0}")]
25    RouteError(String),
26
27    /// Generic error
28    #[error("Error: {0}")]
29    Other(String),
30}
31
32impl Error {
33    /// Create a ServiceNotFound error
34    pub fn service_not_found(service_name: impl Into<String>) -> Self {
35        Self::ServiceNotFound(service_name.into())
36    }
37
38    /// Create a RegistrationError
39    pub fn registration_error(msg: impl Into<String>) -> Self {
40        Self::RegistrationError(msg.into())
41    }
42
43    /// Create a ServerError
44    pub fn server_error(msg: impl Into<String>) -> Self {
45        Self::ServerError(msg.into())
46    }
47
48    /// Create a RouteError
49    pub fn route_error(msg: impl Into<String>) -> Self {
50        Self::RouteError(msg.into())
51    }
52
53    /// Create an Other error
54    pub fn other(msg: impl Into<String>) -> Self {
55        Self::Other(msg.into())
56    }
57}