supabase_client_core/
error.rs1use std::fmt;
2
3#[derive(Debug, thiserror::Error)]
5pub enum SupabaseError {
6 #[cfg(feature = "direct-sql")]
7 #[error("Database error: {0}")]
8 Database(#[from] sqlx::Error),
9
10 #[error("PostgREST error ({status}): {message}")]
11 PostgRest {
12 status: u16,
13 message: String,
14 code: Option<String>,
15 },
16
17 #[error("HTTP error: {0}")]
18 Http(String),
19
20 #[error("Query builder error: {0}")]
21 QueryBuilder(String),
22
23 #[error("Serialization error: {0}")]
24 Serialization(String),
25
26 #[error("Expected exactly one row, but got none")]
27 NoRows,
28
29 #[error("Expected at most one row, but got {0}")]
30 MultipleRows(usize),
31
32 #[error("Configuration error: {0}")]
33 Config(String),
34
35 #[error("Auth error: {0}")]
36 Auth(String),
37
38 #[error("Storage error: {0}")]
39 Storage(String),
40
41 #[error("Realtime error: {0}")]
42 Realtime(String),
43
44 #[error("Functions error: {0}")]
45 Functions(String),
46}
47
48impl SupabaseError {
49 pub fn query_builder(msg: impl Into<String>) -> Self {
50 Self::QueryBuilder(msg.into())
51 }
52
53 pub fn serialization(msg: impl Into<String>) -> Self {
54 Self::Serialization(msg.into())
55 }
56
57 pub fn config(msg: impl Into<String>) -> Self {
58 Self::Config(msg.into())
59 }
60
61 pub fn postgrest(status: u16, message: impl Into<String>, code: Option<String>) -> Self {
62 Self::PostgRest {
63 status,
64 message: message.into(),
65 code,
66 }
67 }
68}
69
70impl From<serde_json::Error> for SupabaseError {
71 fn from(e: serde_json::Error) -> Self {
72 Self::Serialization(e.to_string())
73 }
74}
75
76impl From<reqwest::Error> for SupabaseError {
77 fn from(e: reqwest::Error) -> Self {
78 Self::Http(e.to_string())
79 }
80}
81
82pub type SupabaseResult<T> = Result<T, SupabaseError>;
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq)]
87pub enum StatusCode {
88 Ok = 200,
89 Created = 201,
90 NoContent = 204,
91 NotFound = 404,
92 Conflict = 409,
93 InternalError = 500,
94}
95
96impl fmt::Display for StatusCode {
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 match self {
99 Self::Ok => write!(f, "200 OK"),
100 Self::Created => write!(f, "201 Created"),
101 Self::NoContent => write!(f, "204 No Content"),
102 Self::NotFound => write!(f, "404 Not Found"),
103 Self::Conflict => write!(f, "409 Conflict"),
104 Self::InternalError => write!(f, "500 Internal Server Error"),
105 }
106 }
107}