firebase_rs_sdk/app/
errors.rs

1use std::fmt;
2
3use crate::component::types::ComponentError as ProviderComponentError;
4
5pub type AppResult<T> = Result<T, AppError>;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum AppError {
9    NoApp { app_name: String },
10    BadAppName { app_name: String },
11    DuplicateApp { app_name: String },
12    AppDeleted { app_name: String },
13    ServerAppDeleted,
14    NoOptions,
15    InvalidAppArgument { app_name: String },
16    InvalidLogArgument,
17    FinalizationRegistryNotSupported,
18    InvalidServerAppEnvironment,
19    ComponentFailure { component: String, message: String },
20}
21
22impl fmt::Display for AppError {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        match self {
25            AppError::NoApp { app_name } => write!(
26                f,
27                "No Firebase App '{app_name}' has been created - call initialize_app() first"
28            ),
29            AppError::BadAppName { app_name } => {
30                write!(f, "Illegal App name: '{app_name}'")
31            }
32            AppError::DuplicateApp { app_name } => write!(
33                f,
34                "Firebase App named '{app_name}' already exists with different options or config"
35            ),
36            AppError::AppDeleted { app_name } => {
37                write!(f, "Firebase App named '{app_name}' already deleted")
38            }
39            AppError::ServerAppDeleted => {
40                write!(f, "Firebase Server App has been deleted")
41            }
42            AppError::NoOptions => write!(
43                f,
44                "Need to provide options when not being deployed to hosting via source."
45            ),
46            AppError::InvalidAppArgument { app_name } => write!(
47                f,
48                "firebase.{app_name}() takes either no argument or a Firebase App instance."
49            ),
50            AppError::InvalidLogArgument => {
51                write!(f, "First argument to on_log must be None or a function.")
52            }
53            AppError::FinalizationRegistryNotSupported => write!(
54                f,
55                "FirebaseServerApp release_on_deref defined but runtime lacks FinalizationRegistry support."
56            ),
57            AppError::InvalidServerAppEnvironment => {
58                write!(
59                    f,
60                    "FirebaseServerApp is not for use in browser environments."
61                )
62            }
63            AppError::ComponentFailure { component, message } => {
64                write!(f, "Component {component} error: {message}")
65            }
66        }
67    }
68}
69
70impl std::error::Error for AppError {}
71
72impl From<ProviderComponentError> for AppError {
73    fn from(err: ProviderComponentError) -> Self {
74        match err {
75            ProviderComponentError::MismatchingComponent { expected, found } => {
76                AppError::ComponentFailure {
77                    component: found,
78                    message: format!("does not satisfy provider for {expected}"),
79                }
80            }
81            ProviderComponentError::ComponentAlreadyProvided { name } => {
82                AppError::ComponentFailure {
83                    component: name,
84                    message: "component already provided".to_string(),
85                }
86            }
87            ProviderComponentError::ComponentNotRegistered { name } => AppError::ComponentFailure {
88                component: name,
89                message: "component not registered".to_string(),
90            },
91            ProviderComponentError::InstanceAlreadyInitialized { name, identifier } => {
92                AppError::ComponentFailure {
93                    component: name,
94                    message: format!("instance {identifier} already initialized"),
95                }
96            }
97            ProviderComponentError::InitializationFailed { name, reason } => {
98                AppError::ComponentFailure {
99                    component: name,
100                    message: reason,
101                }
102            }
103            ProviderComponentError::InstanceUnavailable { name } => AppError::ComponentFailure {
104                component: name,
105                message: "instance unavailable".to_string(),
106            },
107        }
108    }
109}