firebase_rs_sdk/firestore/
error.rs

1use std::error::Error;
2use std::fmt::{Display, Formatter};
3
4#[derive(Clone, Debug, PartialEq, Eq)]
5pub enum FirestoreErrorCode {
6    InvalidArgument,
7    MissingProjectId,
8    Internal,
9    NotFound,
10    PermissionDenied,
11    Unauthenticated,
12    Unavailable,
13    DeadlineExceeded,
14    ResourceExhausted,
15}
16
17impl FirestoreErrorCode {
18    pub fn as_str(&self) -> &'static str {
19        match self {
20            FirestoreErrorCode::InvalidArgument => "firestore/invalid-argument",
21            FirestoreErrorCode::MissingProjectId => "firestore/missing-project-id",
22            FirestoreErrorCode::Internal => "firestore/internal",
23            FirestoreErrorCode::NotFound => "firestore/not-found",
24            FirestoreErrorCode::PermissionDenied => "firestore/permission-denied",
25            FirestoreErrorCode::Unauthenticated => "firestore/unauthenticated",
26            FirestoreErrorCode::Unavailable => "firestore/unavailable",
27            FirestoreErrorCode::DeadlineExceeded => "firestore/deadline-exceeded",
28            FirestoreErrorCode::ResourceExhausted => "firestore/resource-exhausted",
29        }
30    }
31}
32
33#[derive(Clone, Debug)]
34pub struct FirestoreError {
35    pub code: FirestoreErrorCode,
36    message: String,
37}
38
39impl FirestoreError {
40    pub fn new(code: FirestoreErrorCode, message: impl Into<String>) -> Self {
41        Self {
42            code,
43            message: message.into(),
44        }
45    }
46
47    pub fn code_str(&self) -> &'static str {
48        self.code.as_str()
49    }
50}
51
52impl Display for FirestoreError {
53    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
54        write!(f, "{} ({})", self.message, self.code_str())
55    }
56}
57
58impl Error for FirestoreError {}
59
60pub type FirestoreResult<T> = Result<T, FirestoreError>;
61
62pub fn invalid_argument(message: impl Into<String>) -> FirestoreError {
63    FirestoreError::new(FirestoreErrorCode::InvalidArgument, message)
64}
65
66pub fn missing_project_id() -> FirestoreError {
67    FirestoreError::new(
68        FirestoreErrorCode::MissingProjectId,
69        "Firebase options must include a project_id to use Firestore",
70    )
71}
72
73pub fn internal_error(message: impl Into<String>) -> FirestoreError {
74    FirestoreError::new(FirestoreErrorCode::Internal, message)
75}
76
77pub fn not_found(message: impl Into<String>) -> FirestoreError {
78    FirestoreError::new(FirestoreErrorCode::NotFound, message)
79}
80
81pub fn permission_denied(message: impl Into<String>) -> FirestoreError {
82    FirestoreError::new(FirestoreErrorCode::PermissionDenied, message)
83}
84
85pub fn unauthenticated(message: impl Into<String>) -> FirestoreError {
86    FirestoreError::new(FirestoreErrorCode::Unauthenticated, message)
87}
88
89pub fn unavailable(message: impl Into<String>) -> FirestoreError {
90    FirestoreError::new(FirestoreErrorCode::Unavailable, message)
91}
92
93pub fn deadline_exceeded(message: impl Into<String>) -> FirestoreError {
94    FirestoreError::new(FirestoreErrorCode::DeadlineExceeded, message)
95}
96
97pub fn resource_exhausted(message: impl Into<String>) -> FirestoreError {
98    FirestoreError::new(FirestoreErrorCode::ResourceExhausted, message)
99}