Skip to main content

sword_core/injectables/
error.rs

1use crate::ConfigError;
2use axum_responses::JsonResponse;
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum DependencyInjectionError {
7    #[error("Failed to build dependency '{type_name}'\n   ↳ Reason: {reason}")]
8    BuildFailed { type_name: String, reason: String },
9
10    #[error(
11        "Dependency '{type_name}' not found in dependency container\n   ↳ Ensure it's registered before use"
12    )]
13    DependencyNotFound { type_name: String },
14
15    #[error("Failed to inject config: {source}")]
16    ConfigInjectionError {
17        #[from]
18        source: ConfigError,
19    },
20
21    #[error(
22        "Circular dependency detected\n  ↳ Ensure there are no cycles in your dependencies"
23    )]
24    CircularDependency,
25}
26
27impl From<DependencyInjectionError> for JsonResponse {
28    fn from(error: DependencyInjectionError) -> Self {
29        tracing::error!("Dependency injection error: {}", error);
30        JsonResponse::InternalServerError()
31    }
32}
33
34impl From<ConfigError> for JsonResponse {
35    fn from(error: ConfigError) -> Self {
36        match error {
37            ConfigError::KeyNotFound { key } => {
38                tracing::error!("Configuration key not found: {key}");
39                JsonResponse::InternalServerError()
40            }
41
42            _ => JsonResponse::InternalServerError(),
43        }
44    }
45}