1use crate::api::Response;
2use crate::sql::Array;
3use crate::sql::Edges;
4use crate::sql::FromValueError;
5use crate::sql::Object;
6use crate::sql::Thing;
7use crate::sql::Value;
8use serde::Serialize;
9use std::io;
10use std::path::PathBuf;
11use thiserror::Error;
12
13#[derive(Error, Debug)]
15#[non_exhaustive]
16pub enum Error {
17 #[error("{0}")]
19 Query(String),
20
21 #[error("There was an error processing a remote HTTP request: {0}")]
23 Http(String),
24
25 #[error("There was an error processing a remote WS request: {0}")]
27 Ws(String),
28
29 #[error("Unsupported protocol or storage engine, `{0}`")]
31 Scheme(String),
32
33 #[error("Connection uninitialised")]
35 ConnectionUninitialised,
36
37 #[error("Already connected")]
39 AlreadyConnected,
40
41 #[error("Invalid bindings: {0}")]
43 InvalidBindings(Value),
44
45 #[error("Range on record IDs not supported: {0}")]
47 RangeOnRecordId(Thing),
48
49 #[error("Range on objects not supported: {0}")]
51 RangeOnObject(Object),
52
53 #[error("Range on arrays not supported: {0}")]
55 RangeOnArray(Array),
56
57 #[error("Range on edges not supported: {0}")]
59 RangeOnEdges(Edges),
60
61 #[error("`{table}:{id}` is not allowed as a method parameter; try `({table}, {id})`")]
63 TableColonId {
64 table: String,
65 id: String,
66 },
67
68 #[error("Duplicate request ID: {0}")]
70 DuplicateRequestId(i64),
71
72 #[error("Invalid request: {0}")]
74 InvalidRequest(String),
75
76 #[error("Invalid params: {0}")]
78 InvalidParams(String),
79
80 #[error("Internal error: {0}")]
82 InternalError(String),
83
84 #[error("Parse error: {0}")]
86 ParseError(String),
87
88 #[error("Invalid semantic version: {0}")]
90 InvalidSemanticVersion(String),
91
92 #[error("Invalid URL: {0}")]
94 InvalidUrl(String),
95
96 #[error("Failed to convert `{value}` to `T`: {error}")]
98 FromValue {
99 value: Value,
100 error: String,
101 },
102
103 #[error("Failed to deserialize a binary response: {error}")]
105 ResponseFromBinary {
106 binary: Vec<u8>,
107 error: bincode::Error,
108 },
109
110 #[error("Failed to serialize `{value}` to JSON string: {error}")]
112 ToJsonString {
113 value: Value,
114 error: String,
115 },
116
117 #[error("Failed to deserialize `{string}` to sql::Value: {error}")]
119 FromJsonString {
120 string: String,
121 error: String,
122 },
123
124 #[error("Invalid namespace name: {0:?}")]
126 InvalidNsName(String),
127
128 #[error("Invalid database name: {0:?}")]
130 InvalidDbName(String),
131
132 #[error("Failed to open `{path}`: {error}")]
134 FileOpen {
135 path: PathBuf,
136 error: io::Error,
137 },
138
139 #[error("Failed to read `{path}`: {error}")]
141 FileRead {
142 path: PathBuf,
143 error: io::Error,
144 },
145
146 #[error("Tried to take only a single result from a query that contains multiple")]
148 LossyTake(Response),
149
150 #[error("The protocol or storage engine does not support backups on this architecture")]
153 BackupsNotSupported,
154
155 #[error("server version `{server_version}` does not match the range supported by the client `{supported_versions}`")]
157 VersionMismatch {
158 server_version: semver::Version,
159 supported_versions: String,
160 },
161
162 #[error("server build `{server_metadata}` is older than the minimum supported build `{supported_metadata}`")]
164 BuildMetadataMismatch {
165 server_metadata: semver::BuildMetadata,
166 supported_metadata: semver::BuildMetadata,
167 },
168
169 #[error("The protocol or storage engine does not support live queries on this architecture")]
172 LiveQueriesNotSupported,
173
174 #[error("Live queries on objects not supported: {0}")]
176 LiveOnObject(Object),
177
178 #[error("Live queries on arrays not supported: {0}")]
180 LiveOnArray(Array),
181
182 #[error("Live queries on edges not supported: {0}")]
184 LiveOnEdges(Edges),
185
186 #[error("Query statement {0} is not a live query")]
188 NotLiveQuery(usize),
189
190 #[error("Query statement {0} is out of bounds")]
192 QueryIndexOutOfBounds(usize),
193
194 #[error("Tried to take a query response that has already been taken")]
196 ResponseAlreadyTaken,
197
198 #[error("Insert queries on objects not supported: {0}")]
200 InsertOnObject(Object),
201
202 #[error("Insert queries on arrays not supported: {0}")]
204 InsertOnArray(Array),
205
206 #[error("Insert queries on edges not supported: {0}")]
208 InsertOnEdges(Edges),
209}
210
211#[cfg(feature = "protocol-http")]
212impl From<reqwest::Error> for crate::Error {
213 fn from(e: reqwest::Error) -> Self {
214 Self::Api(Error::Http(e.to_string()))
215 }
216}
217
218#[cfg(all(feature = "protocol-ws", not(target_arch = "wasm32")))]
219#[cfg_attr(docsrs, doc(cfg(all(feature = "protocol-ws", not(target_arch = "wasm32")))))]
220impl From<tokio_tungstenite::tungstenite::Error> for crate::Error {
221 fn from(error: tokio_tungstenite::tungstenite::Error) -> Self {
222 Self::Api(Error::Ws(error.to_string()))
223 }
224}
225
226impl<T> From<flume::SendError<T>> for crate::Error {
227 fn from(error: flume::SendError<T>) -> Self {
228 Self::Api(Error::InternalError(error.to_string()))
229 }
230}
231
232impl From<flume::RecvError> for crate::Error {
233 fn from(error: flume::RecvError) -> Self {
234 Self::Api(Error::InternalError(error.to_string()))
235 }
236}
237
238impl From<url::ParseError> for crate::Error {
239 fn from(error: url::ParseError) -> Self {
240 Self::Api(Error::InternalError(error.to_string()))
241 }
242}
243
244#[cfg(all(feature = "protocol-ws", target_arch = "wasm32"))]
245#[cfg_attr(docsrs, doc(cfg(all(feature = "protocol-ws", target_arch = "wasm32"))))]
246impl From<ws_stream_wasm::WsErr> for crate::Error {
247 fn from(error: ws_stream_wasm::WsErr) -> Self {
248 Self::Api(Error::Ws(error.to_string()))
249 }
250}
251
252#[cfg(all(feature = "protocol-ws", target_arch = "wasm32"))]
253#[cfg_attr(docsrs, doc(cfg(all(feature = "protocol-ws", target_arch = "wasm32"))))]
254impl From<pharos::PharErr> for crate::Error {
255 fn from(error: pharos::PharErr) -> Self {
256 Self::Api(Error::Ws(error.to_string()))
257 }
258}
259
260impl Serialize for Error {
261 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
262 where
263 S: serde::Serializer,
264 {
265 serializer.serialize_str(self.to_string().as_str())
266 }
267}
268
269impl From<FromValueError> for crate::Error {
270 fn from(error: FromValueError) -> Self {
271 Self::Api(Error::FromValue {
272 value: error.value,
273 error: error.error,
274 })
275 }
276}