Skip to main content

cdk_supabase/
error.rs

1use cdk_common::database::Error as DatabaseError;
2use thiserror::Error;
3
4/// Errors that can occur when interacting with Supabase
5#[derive(Debug, Error)]
6pub enum Error {
7    /// Database error
8    #[error(transparent)]
9    Database(#[from] DatabaseError),
10    /// HTTP request error
11    #[error(transparent)]
12    Reqwest(#[from] reqwest::Error),
13    /// URL parsing error
14    #[error(transparent)]
15    Url(#[from] url::ParseError),
16    /// JSON serialization/deserialization error
17    #[error(transparent)]
18    Serde(#[from] serde_json::Error),
19    /// Supabase-specific error
20    #[error("Supabase error: {0}")]
21    Supabase(String),
22    /// Schema version mismatch — the database schema is outdated
23    #[error(
24        "Schema version mismatch: SDK requires version {required}, \
25         database has version {found}. \
26         An administrator must run migrations to update the database schema."
27    )]
28    SchemaMismatch {
29        /// The schema version required by this SDK version
30        required: u32,
31        /// The schema version found in the database
32        found: u32,
33    },
34    /// Schema not initialized — the database has no schema_info table
35    #[error(
36        "Database schema not initialized. \
37         An administrator must run the initial migrations before clients can connect. \
38         Use `SupabaseWalletDatabase::get_schema_sql()` to get the required SQL."
39    )]
40    SchemaNotInitialized,
41}
42
43impl From<Error> for DatabaseError {
44    fn from(e: Error) -> Self {
45        match e {
46            Error::Database(e) => e,
47            Error::Reqwest(e) => DatabaseError::Database(Box::new(e)),
48            Error::Url(e) => DatabaseError::Database(Box::new(e)),
49            Error::Serde(e) => DatabaseError::Database(Box::new(e)),
50            Error::Supabase(msg) => DatabaseError::Database(Box::new(std::io::Error::other(msg))),
51            Error::SchemaMismatch { required, found } => {
52                DatabaseError::Database(Box::new(std::io::Error::other(format!(
53                    "Schema version mismatch: SDK requires version {required}, \
54                     database has version {found}. \
55                     An administrator must run migrations to update the database schema."
56                ))))
57            }
58            Error::SchemaNotInitialized => {
59                DatabaseError::Database(Box::new(std::io::Error::other(
60                    "Database schema not initialized. \
61                     An administrator must run the initial migrations before clients can connect.",
62                )))
63            }
64        }
65    }
66}