supabase_client_rs/
error.rs

1//! Error types for the Supabase client.
2
3use thiserror::Error;
4
5/// The main error type for Supabase operations.
6#[derive(Debug, Error)]
7pub enum Error {
8    /// Configuration error (missing URL, invalid key, etc.)
9    #[error("configuration error: {0}")]
10    Config(String),
11
12    /// URL parsing error
13    #[error("invalid URL: {0}")]
14    Url(#[from] url::ParseError),
15
16    /// HTTP request error
17    #[error("HTTP error: {0}")]
18    Http(#[from] reqwest::Error),
19
20    /// JSON serialization/deserialization error
21    #[error("JSON error: {0}")]
22    Json(#[from] serde_json::Error),
23
24    /// PostgREST-specific error
25    #[error("PostgREST error: {message}")]
26    PostgREST {
27        /// The error message
28        message: String,
29        /// Optional error code
30        code: Option<String>,
31        /// Optional error details
32        details: Option<String>,
33        /// Optional hint for fixing the error
34        hint: Option<String>,
35    },
36
37    /// Authentication error
38    #[error("authentication error: {0}")]
39    Auth(String),
40
41    /// Storage error
42    #[error("storage error: {0}")]
43    Storage(String),
44
45    /// Realtime connection error
46    #[error("realtime error: {0}")]
47    Realtime(String),
48
49    /// Edge function invocation error
50    #[error("function error: {0}")]
51    Function(String),
52
53    /// Feature not available (crate not enabled)
54    #[error("{0} is not available - enable the '{1}' feature")]
55    FeatureNotEnabled(&'static str, &'static str),
56}
57
58/// A specialized Result type for Supabase operations.
59pub type Result<T> = std::result::Result<T, Error>;
60
61impl Error {
62    /// Create a configuration error.
63    pub fn config(msg: impl Into<String>) -> Self {
64        Self::Config(msg.into())
65    }
66
67    /// Create a PostgREST error from response details.
68    pub fn postgrest(
69        message: impl Into<String>,
70        code: Option<String>,
71        details: Option<String>,
72        hint: Option<String>,
73    ) -> Self {
74        Self::PostgREST {
75            message: message.into(),
76            code,
77            details,
78            hint,
79        }
80    }
81}
82
83/// Convert RealtimeError to our Error type when the realtime feature is enabled
84#[cfg(feature = "realtime")]
85impl From<supabase_realtime_rs::RealtimeError> for Error {
86    fn from(err: supabase_realtime_rs::RealtimeError) -> Self {
87        Self::Realtime(err.to_string())
88    }
89}