Skip to main content

rok_container/
error.rs

1use thiserror::Error;
2
3/// Errors returned by [`Container`](crate::Container) operations and Axum extractors.
4#[derive(Error, Debug)]
5pub enum ContainerError {
6    /// The requested type was never registered with `bind()` or `singleton()`.
7    #[error("service not registered: {0}")]
8    NotRegistered(&'static str),
9
10    /// Internal type-map downcast failed — indicates a container bug, not user error.
11    #[error("type mismatch when resolving: {0}")]
12    TypeMismatch(&'static str),
13
14    /// A required route path parameter was absent.
15    #[error("missing route parameter: {0}")]
16    MissingRouteParam(&'static str),
17
18    /// Route model binding found no record matching the route key.
19    #[error("model not found")]
20    ModelNotFound,
21}
22
23#[cfg(feature = "axum")]
24impl axum::response::IntoResponse for ContainerError {
25    fn into_response(self) -> axum::response::Response {
26        use axum::http::StatusCode;
27
28        let status = match &self {
29            ContainerError::ModelNotFound => StatusCode::NOT_FOUND,
30            _ => StatusCode::INTERNAL_SERVER_ERROR,
31        };
32
33        axum::response::IntoResponse::into_response((status, self.to_string()))
34    }
35}