1use std::io;
2use std::path::PathBuf;
3
4use serde::Serialize;
5use thiserror::Error;
6
7use crate::Value;
8use crate::api::Response;
9use crate::core::dbs::capabilities::{ParseFuncTargetError, ParseNetTargetError};
10
11#[derive(Error, Debug)]
13#[non_exhaustive]
14pub enum Error {
15 #[error("{0}")]
17 Query(String),
18
19 #[error("There was an error processing a remote HTTP request: {0}")]
21 Http(String),
22
23 #[error("There was an error processing a remote WS request: {0}")]
25 Ws(String),
26
27 #[error("Unsupported protocol or storage engine, `{0}`")]
30 Scheme(String),
31
32 #[error("Connection uninitialised")]
34 ConnectionUninitialised,
35
36 #[error("Already connected")]
38 AlreadyConnected,
39
40 #[error("Invalid bindings: {0}")]
42 InvalidBindings(Value),
43
44 #[error("Tried to add a range to an record-id resource")]
46 RangeOnRecordId,
47
48 #[error("Tried to add a range to an object resource")]
50 RangeOnObject,
51
52 #[error("Tried to add a range to an array resource")]
54 RangeOnArray,
55
56 #[error("Tried to add a range to an edge resource")]
58 RangeOnEdges,
59
60 #[error("Tried to add a range to a resource which was already a range")]
62 RangeOnRange,
63
64 #[error("Tried to add a range to an unspecified resource")]
66 RangeOnUnspecified,
67
68 #[error(
71 "Table name `{table}` contained a colon (:), this is dissallowed to avoid confusion with record-id's try `Table(\"{table}\")` instead."
72 )]
73 TableColonId {
74 table: String,
75 },
76
77 #[error("Duplicate request ID: {0}")]
79 DuplicateRequestId(i64),
80
81 #[error("Invalid request: {0}")]
83 InvalidRequest(String),
84
85 #[error("Invalid params: {0}")]
87 InvalidParams(String),
88
89 #[error("Internal error: {0}")]
91 InternalError(String),
92
93 #[error("Parse error: {0}")]
95 ParseError(String),
96
97 #[error("Invalid semantic version: {0}")]
99 InvalidSemanticVersion(String),
100
101 #[error("Invalid URL: {0}")]
103 InvalidUrl(String),
104
105 #[error("Failed to convert `{value}` to `T`: {error}")]
107 FromValue {
108 value: Value,
109 error: String,
110 },
111
112 #[error("Failed to deserialize a binary response: {error}")]
114 ResponseFromBinary {
115 binary: Vec<u8>,
116 error: bincode::Error,
117 },
118
119 #[error("Failed to serialize `{value}` to JSON string: {error}")]
121 ToJsonString {
122 value: Value,
123 error: String,
124 },
125
126 #[error("Failed to deserialize `{string}` to sql::Value: {error}")]
128 FromJsonString {
129 string: String,
130 error: String,
131 },
132
133 #[error("Invalid namespace name: {0:?}")]
135 InvalidNsName(String),
136
137 #[error("Invalid database name: {0:?}")]
139 InvalidDbName(String),
140
141 #[error("Failed to open `{path}`: {error}")]
143 FileOpen {
144 path: PathBuf,
145 error: io::Error,
146 },
147
148 #[error("Failed to read `{path}`: {error}")]
150 FileRead {
151 path: PathBuf,
152 error: io::Error,
153 },
154
155 #[error("Tried to take only a single result from a query that contains multiple")]
158 LossyTake(Response),
159
160 #[error("The protocol or storage engine does not support backups on this architecture")]
163 BackupsNotSupported,
164
165 #[error(
168 "server version `{server_version}` does not match the range supported by the client `{supported_versions}`"
169 )]
170 VersionMismatch {
171 server_version: semver::Version,
172 supported_versions: String,
173 },
174
175 #[error(
178 "server build `{server_metadata}` is older than the minimum supported build `{supported_metadata}`"
179 )]
180 BuildMetadataMismatch {
181 server_metadata: semver::BuildMetadata,
182 supported_metadata: semver::BuildMetadata,
183 },
184
185 #[error("The protocol or storage engine does not support live queries on this architecture")]
188 LiveQueriesNotSupported,
189
190 #[error("Live queries on objects not supported")]
192 LiveOnObject,
193
194 #[error("Live queries on arrays not supported")]
196 LiveOnArray,
197
198 #[error("Live queries on edges not supported")]
200 LiveOnEdges,
201
202 #[error("Live queries on unspecified resource not supported")]
204 LiveOnUnspecified,
205
206 #[error("Query statement {0} is not a live query")]
209 NotLiveQuery(usize),
210
211 #[error("Query statement {0} is out of bounds")]
214 QueryIndexOutOfBounds(usize),
215
216 #[error("Tried to take a query response that has already been taken")]
219 ResponseAlreadyTaken,
220
221 #[error("Insert queries on objects are not supported")]
223 InsertOnObject,
224
225 #[error("Insert queries on arrays are not supported")]
227 InsertOnArray,
228
229 #[error("Insert queries on edges are not supported")]
231 InsertOnEdges,
232
233 #[error("Insert queries on ranges are not supported")]
235 InsertOnRange,
236
237 #[error("Insert queries on unspecified resource with no data are not supported")]
239 InsertOnUnspecified,
240
241 #[error("Crendentials for signin and signup should be an object")]
242 CrendentialsNotObject,
243
244 #[error("{0}")]
245 InvalidNetTarget(#[from] ParseNetTargetError),
246
247 #[error("{0}")]
248 InvalidFuncTarget(#[from] ParseFuncTargetError),
249
250 #[error("failed to serialize Value: {0}")]
251 SerializeValue(String),
252 #[error("failed to deserialize Value: {0}")]
253 DeSerializeValue(String),
254
255 #[error("failed to serialize to a Value: {0}")]
256 Serializer(String),
257 #[error("failed to deserialize from a Value: {0}")]
258 Deserializer(String),
259
260 #[error("The server returned an unexpected response: {0}")]
261 InvalidResponse(String),
262
263 #[error("Tried to send a value which could not be serialized: {0}")]
264 UnserializableValue(String),
265
266 #[error(
269 "tried to convert from a value which contained non-primitive values to a value which only allows primitive values."
270 )]
271 ReceivedInvalidValue,
272
273 #[error("The '{0}' engine does not support data versioning")]
275 VersionsNotSupported(String),
276}
277
278impl serde::ser::Error for Error {
279 fn custom<T>(msg: T) -> Self
280 where
281 T: std::fmt::Display,
282 {
283 Error::SerializeValue(msg.to_string())
284 }
285}
286
287impl serde::de::Error for Error {
288 fn custom<T>(msg: T) -> Self
289 where
290 T: std::fmt::Display,
291 {
292 Error::DeSerializeValue(msg.to_string())
293 }
294}
295
296impl Serialize for Error {
297 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
298 where
299 S: serde::Serializer,
300 {
301 serializer.serialize_str(self.to_string().as_str())
302 }
303}