use cdk_common::database::Error as DatabaseError;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error(transparent)]
Database(#[from] DatabaseError),
#[error(transparent)]
Reqwest(#[from] reqwest::Error),
#[error(transparent)]
Url(#[from] url::ParseError),
#[error(transparent)]
Serde(#[from] serde_json::Error),
#[error("Supabase error: {0}")]
Supabase(String),
#[error(
"Schema version mismatch: SDK requires version {required}, \
database has version {found}. \
An administrator must run migrations to update the database schema."
)]
SchemaMismatch {
required: u32,
found: u32,
},
#[error(
"Database schema not initialized. \
An administrator must run the initial migrations before clients can connect. \
Use `SupabaseWalletDatabase::get_schema_sql()` to get the required SQL."
)]
SchemaNotInitialized,
}
impl From<Error> for DatabaseError {
fn from(e: Error) -> Self {
match e {
Error::Database(e) => e,
Error::Reqwest(e) => DatabaseError::Database(Box::new(e)),
Error::Url(e) => DatabaseError::Database(Box::new(e)),
Error::Serde(e) => DatabaseError::Database(Box::new(e)),
Error::Supabase(msg) => DatabaseError::Database(Box::new(std::io::Error::other(msg))),
Error::SchemaMismatch { required, found } => {
DatabaseError::Database(Box::new(std::io::Error::other(format!(
"Schema version mismatch: SDK requires version {required}, \
database has version {found}. \
An administrator must run migrations to update the database schema."
))))
}
Error::SchemaNotInitialized => {
DatabaseError::Database(Box::new(std::io::Error::other(
"Database schema not initialized. \
An administrator must run the initial migrations before clients can connect.",
)))
}
}
}
}