1use thiserror::Error;
2
3#[derive(Error, Debug)]
4#[non_exhaustive]
5pub enum Error {
6 #[error("SQLite error: {0}")]
7 Sqlite(#[from] rusqlite::Error),
8
9 #[error("Object store error: {0}")]
10 ObjectStore(#[from] object_store::Error),
11
12 #[error("WAL parse error at offset {offset}: {detail}")]
13 WalParse { offset: u64, detail: String },
14
15 #[error("WAL checksum mismatch at frame {frame}: ck1 stored {stored_1:#x}, computed {computed_1:#x}; ck2 stored {stored_2:#x}, computed {computed_2:#x}")]
16 WalChecksum {
17 frame: u32,
18 stored_1: u32,
19 computed_1: u32,
20 stored_2: u32,
21 computed_2: u32,
22 },
23
24 #[error("Restore error: {0}")]
25 Restore(String),
26
27 #[error("IO error: {0}")]
28 Io(#[from] std::io::Error),
29
30 #[error("Replication error: {0}")]
31 Replication(String),
32
33 #[error("Manifest conflict: remote is ahead (gen {remote_gen} vs local {local_gen}). Re-restore required.")]
34 ManifestConflict { local_gen: u64, remote_gen: u64 },
35
36 #[error("Snapshot error: {0}")]
37 Snapshot(String),
38
39 #[error("Configuration error: {0}")]
40 Config(String),
41
42 #[error("Serialization error: {0}")]
43 Serialization(#[from] serde_json::Error),
44
45 #[error("Task join error: {0}")]
46 Join(#[from] tokio::task::JoinError),
47
48 #[error("{0}")]
49 Other(String),
50
51 #[error(
52 "Precondition failed for key '{key}': expected etag '{expected}', object was modified"
53 )]
54 PreconditionFailed { key: String, expected: String },
55
56 #[error("Compaction error: {0}")]
57 Compaction(String),
58
59 #[error("Database locked by another process: {path} (PID {holder_pid})")]
60 DatabaseLocked {
61 path: std::path::PathBuf,
62 holder_pid: u32,
63 },
64
65 #[error("Replica locked by another process: prefix '{prefix}' held by PID {holder_pid} on {holder_hostname} (last heartbeat {last_heartbeat})")]
66 ReplicaLocked {
67 prefix: String,
68 holder_pid: u32,
69 holder_hostname: String,
70 last_heartbeat: String,
71 },
72 #[error("THIS PREFIX BELONGS TO ANOTHER SERVER: prefix '{prefix}' is owned by '{expected_writer}', you are '{actual_writer}'. Do NOT share bucket prefixes between servers. Each server gets its own prefix.")]
80 WrongWriter {
81 prefix: String,
82 expected_writer: String,
83 actual_writer: String,
84 },
85}
86
87impl Error {
88 pub fn is_not_found(&self) -> bool {
89 match self {
90 Error::ObjectStore(e) => matches!(e, object_store::Error::NotFound { .. }),
91 _ => false,
92 }
93 }
94
95 pub fn is_io_not_found(&self) -> bool {
96 match self {
97 Error::Io(e) => e.kind() == std::io::ErrorKind::NotFound,
98 _ => false,
99 }
100 }
101
102 pub fn is_retryable(&self) -> bool {
105 match self {
106 Error::ObjectStore(e) => !matches!(e, object_store::Error::NotFound { .. }),
107 Error::Io(e) => matches!(
108 e.kind(),
109 std::io::ErrorKind::Interrupted
110 | std::io::ErrorKind::WouldBlock
111 | std::io::ErrorKind::ConnectionRefused
112 | std::io::ErrorKind::ConnectionReset
113 | std::io::ErrorKind::ConnectionAborted
114 | std::io::ErrorKind::TimedOut
115 | std::io::ErrorKind::NotConnected
116 ),
117 Error::PreconditionFailed { .. } => false,
118 Error::ManifestConflict { .. } => false,
119 Error::Config(_) => false,
120 Error::WalParse { .. } => false,
121 Error::WalChecksum { .. } => false,
122 Error::DatabaseLocked { .. } => false,
123 Error::ReplicaLocked { .. } => false,
124 Error::WrongWriter { .. } => false,
125 Error::Sqlite(_) => true,
129 Error::Restore(_) => false,
131 Error::Replication(_) => true,
133 Error::Snapshot(_) => true,
135 Error::Serialization(_) => false,
137 Error::Join(_) => false,
139 Error::Other(_) => false,
141 Error::Compaction(_) => true,
143 }
144 }
145
146 pub fn is_database_locked(&self) -> bool {
148 matches!(self, Error::DatabaseLocked { .. })
149 }
150
151 pub fn is_replica_locked(&self) -> bool {
153 matches!(self, Error::ReplicaLocked { .. })
154 }
155
156 pub fn is_wrong_writer(&self) -> bool {
157 matches!(self, Error::WrongWriter { .. })
158 }
159
160 pub fn is_precondition_failed(&self) -> bool {
162 matches!(self, Error::PreconditionFailed { .. })
163 }
164}
165
166pub type Result<T> = std::result::Result<T, Error>;