firebase_rs_sdk/functions/
error.rs

1use std::fmt::{Display, Formatter};
2
3#[derive(Clone, Debug, PartialEq, Eq)]
4pub enum FunctionsErrorCode {
5    Internal,
6    InvalidArgument,
7}
8
9impl FunctionsErrorCode {
10    pub fn as_str(&self) -> &'static str {
11        match self {
12            FunctionsErrorCode::Internal => "functions/internal",
13            FunctionsErrorCode::InvalidArgument => "functions/invalid-argument",
14        }
15    }
16}
17
18#[derive(Clone, Debug)]
19pub struct FunctionsError {
20    pub code: FunctionsErrorCode,
21    message: String,
22}
23
24impl FunctionsError {
25    pub fn new(code: FunctionsErrorCode, message: impl Into<String>) -> Self {
26        Self {
27            code,
28            message: message.into(),
29        }
30    }
31
32    pub fn code_str(&self) -> &'static str {
33        self.code.as_str()
34    }
35}
36
37impl Display for FunctionsError {
38    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
39        write!(f, "{} ({})", self.message, self.code_str())
40    }
41}
42
43impl std::error::Error for FunctionsError {}
44
45pub type FunctionsResult<T> = Result<T, FunctionsError>;
46
47pub fn invalid_argument(message: impl Into<String>) -> FunctionsError {
48    FunctionsError::new(FunctionsErrorCode::InvalidArgument, message)
49}
50
51pub fn internal_error(message: impl Into<String>) -> FunctionsError {
52    FunctionsError::new(FunctionsErrorCode::Internal, message)
53}