Skip to main content

sqlrite/
error.rs

1use thiserror::Error;
2
3use std::result;
4
5use sqlparser::parser::ParserError;
6
7/// This is a type that encapsulated the `std::result` with the enum `SQLRiteError`
8/// and makes function signatures easier to read.
9pub type Result<T> = result::Result<T, SQLRiteError>;
10
11/// SQLRiteError is an enum with all the standardized errors available for returning
12///
13#[derive(Error, Debug)]
14pub enum SQLRiteError {
15    #[error("Not Implemented error: {0}")]
16    NotImplemented(String),
17    #[error("General error: {0}")]
18    General(String),
19    #[error("Internal error: {0}")]
20    Internal(String),
21    #[error("Unknown command error: {0}")]
22    UnknownCommand(String),
23    #[error("SQL error: {0:?}")]
24    SqlError(#[from] ParserError),
25    #[error("IO error: {0}")]
26    Io(#[from] std::io::Error),
27    /// Phase 11.4 — `BEGIN CONCURRENT` commit hit a write-write
28    /// conflict. Some other transaction committed a newer version
29    /// of a row in this transaction's write-set after this
30    /// transaction's `begin_ts`. Caller should `ROLLBACK` (already
31    /// implicitly performed) and retry the transaction with a
32    /// fresh `begin_ts`.
33    #[error("Busy: {0}")]
34    Busy(String),
35    /// Phase 11.4 — same shape as [`SQLRiteError::Busy`] but
36    /// surfaces the snapshot-isolation specific case: a row in
37    /// the read-set changed under us. Distinguished from `Busy`
38    /// so SDKs can map it to a per-language exception that the
39    /// caller's retry helper recognizes (mirrors Turso /
40    /// libSQL's `BUSY` vs `BUSY_SNAPSHOT` split). v0 only emits
41    /// `Busy` from the write-write validation loop; the
42    /// read-anomaly variant is reserved for the snapshot-read
43    /// integration that follows.
44    #[error("BusySnapshot: {0}")]
45    BusySnapshot(String),
46}
47
48// `std::io::Error` has no `PartialEq`, so we implement one by value-of-message.
49// Used by existing tests that compare error variants.
50impl PartialEq for SQLRiteError {
51    fn eq(&self, other: &Self) -> bool {
52        use SQLRiteError::*;
53        match (self, other) {
54            (NotImplemented(a), NotImplemented(b)) => a == b,
55            (General(a), General(b)) => a == b,
56            (Internal(a), Internal(b)) => a == b,
57            (UnknownCommand(a), UnknownCommand(b)) => a == b,
58            (SqlError(a), SqlError(b)) => format!("{a:?}") == format!("{b:?}"),
59            (Io(a), Io(b)) => a.kind() == b.kind() && a.to_string() == b.to_string(),
60            (Busy(a), Busy(b)) => a == b,
61            (BusySnapshot(a), BusySnapshot(b)) => a == b,
62            _ => false,
63        }
64    }
65}
66
67impl SQLRiteError {
68    /// Phase 11.4 — true for `Busy` and `BusySnapshot`. SDK retry
69    /// helpers branch on this rather than matching the variants
70    /// individually so adding a third "retryable" variant later
71    /// doesn't break callers.
72    pub fn is_retryable(&self) -> bool {
73        matches!(self, SQLRiteError::Busy(_) | SQLRiteError::BusySnapshot(_))
74    }
75}
76
77/// Returns SQLRiteError::General error from String
78#[allow(dead_code)]
79pub fn sqlrite_error(message: &str) -> SQLRiteError {
80    SQLRiteError::General(message.to_owned())
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    #[test]
88    fn sqlrite_error_test() {
89        let input = String::from("test error");
90        let expected = SQLRiteError::General("test error".to_string());
91
92        let result = sqlrite_error(&input);
93        assert_eq!(result, expected);
94    }
95
96    #[test]
97    fn sqlrite_display_not_implemented_test() {
98        let error_string = String::from("Feature not implemented.");
99        let input = SQLRiteError::NotImplemented(error_string.clone());
100
101        let expected = format!("Not Implemented error: {}", error_string);
102        let result = format!("{}", input);
103        assert_eq!(result, expected);
104    }
105
106    #[test]
107    fn sqlrite_display_general_test() {
108        let error_string = String::from("General error.");
109        let input = SQLRiteError::General(error_string.clone());
110
111        let expected = format!("General error: {}", error_string);
112        let result = format!("{}", input);
113        assert_eq!(result, expected);
114    }
115
116    #[test]
117    fn sqlrite_display_internal_test() {
118        let error_string = String::from("Internet error.");
119        let input = SQLRiteError::Internal(error_string.clone());
120
121        let expected = format!("Internal error: {}", error_string);
122        let result = format!("{}", input);
123        assert_eq!(result, expected);
124    }
125
126    #[test]
127    fn sqlrite_display_sqlrite_test() {
128        let error_string = String::from("SQL error.");
129        let input = SQLRiteError::SqlError(ParserError::ParserError(error_string.clone()));
130
131        let expected = format!("SQL error: ParserError(\"{}\")", error_string);
132        let result = format!("{}", input);
133        assert_eq!(result, expected);
134    }
135
136    #[test]
137    fn sqlrite_unknown_test() {
138        let error_string = String::from("Unknown error.");
139        let input = SQLRiteError::UnknownCommand(error_string.clone());
140
141        let expected = format!("Unknown command error: {}", error_string);
142        let result = format!("{}", input);
143        assert_eq!(result, expected);
144    }
145}