1use crate::api::Response;
2use crate::sql::Array;
3use crate::sql::Edges;
4use crate::sql::Object;
5use crate::sql::Thing;
6use crate::sql::Value;
7use serde::Serialize;
8use std::io;
9use std::path::PathBuf;
10use thiserror::Error;
11
12#[derive(Error, Debug)]
14#[non_exhaustive]
15pub enum Error {
16 #[error("{0}")]
18 Query(String),
19
20 #[error("There was an error processing a remote HTTP request: {0}")]
22 Http(String),
23
24 #[error("There was an error processing a remote WS request: {0}")]
26 Ws(String),
27
28 #[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("Range on record IDs not supported: {0}")]
46 RangeOnRecordId(Thing),
47
48 #[error("Range on objects not supported: {0}")]
50 RangeOnObject(Object),
51
52 #[error("Range on arrays not supported: {0}")]
54 RangeOnArray(Array),
55
56 #[error("Range on edges not supported: {0}")]
58 RangeOnEdges(Edges),
59
60 #[error("`{table}:{id}` is not allowed as a method parameter; try `({table}, {id})`")]
62 TableColonId {
63 table: String,
64 id: String,
65 },
66
67 #[error("Duplicate request ID: {0}")]
69 DuplicateRequestId(i64),
70
71 #[error("Invalid request: {0}")]
73 InvalidRequest(String),
74
75 #[error("Invalid params: {0}")]
77 InvalidParams(String),
78
79 #[error("Internal error: {0}")]
81 InternalError(String),
82
83 #[error("Parse error: {0}")]
85 ParseError(String),
86
87 #[error("Invalid semantic version: {0}")]
89 InvalidSemanticVersion(String),
90
91 #[error("Invalid URL: {0}")]
93 InvalidUrl(String),
94
95 #[error("Failed to convert `{value}` to `T`: {error}")]
97 FromValue {
98 value: Value,
99 error: String,
100 },
101
102 #[error("Failed to deserialize a binary response: {error}")]
104 ResponseFromBinary {
105 binary: Vec<u8>,
106 error: bincode::Error,
107 },
108
109 #[error("Failed to serialize `{value}` to JSON string: {error}")]
111 ToJsonString {
112 value: Value,
113 error: String,
114 },
115
116 #[error("Failed to deserialize `{string}` to sql::Value: {error}")]
118 FromJsonString {
119 string: String,
120 error: String,
121 },
122
123 #[error("Invalid namespace name: {0:?}")]
125 InvalidNsName(String),
126
127 #[error("Invalid database name: {0:?}")]
129 InvalidDbName(String),
130
131 #[error("Failed to open `{path}`: {error}")]
133 FileOpen {
134 path: PathBuf,
135 error: io::Error,
136 },
137
138 #[error("Failed to read `{path}`: {error}")]
140 FileRead {
141 path: PathBuf,
142 error: io::Error,
143 },
144
145 #[error("Tried to take only a single result from a query that contains multiple")]
147 LossyTake(Response),
148
149 #[error("The protocol or storage engine does not support backups on this architecture")]
152 BackupsNotSupported,
153
154 #[error("server version `{server_version}` does not match the range supported by the client `{supported_versions}`")]
156 VersionMismatch {
157 server_version: semver::Version,
158 supported_versions: String,
159 },
160
161 #[error("server build `{server_metadata}` is older than the minimum supported build `{supported_metadata}`")]
163 BuildMetadataMismatch {
164 server_metadata: semver::BuildMetadata,
165 supported_metadata: semver::BuildMetadata,
166 },
167
168 #[error("The protocol or storage engine does not support live queries on this architecture")]
171 LiveQueriesNotSupported,
172
173 #[error("Live queries on objects not supported: {0}")]
175 LiveOnObject(Object),
176
177 #[error("Live queries on arrays not supported: {0}")]
179 LiveOnArray(Array),
180
181 #[error("Live queries on edges not supported: {0}")]
183 LiveOnEdges(Edges),
184}
185
186#[cfg(feature = "protocol-http")]
187impl From<reqwest::Error> for crate::Error {
188 fn from(e: reqwest::Error) -> Self {
189 Self::Api(Error::Http(e.to_string()))
190 }
191}
192
193#[cfg(all(feature = "protocol-ws", not(target_arch = "wasm32")))]
194#[cfg_attr(docsrs, doc(cfg(all(feature = "protocol-ws", not(target_arch = "wasm32")))))]
195impl From<tokio_tungstenite::tungstenite::Error> for crate::Error {
196 fn from(error: tokio_tungstenite::tungstenite::Error) -> Self {
197 Self::Api(Error::Ws(error.to_string()))
198 }
199}
200
201impl<T> From<flume::SendError<T>> for crate::Error {
202 fn from(error: flume::SendError<T>) -> Self {
203 Self::Api(Error::InternalError(error.to_string()))
204 }
205}
206
207impl From<flume::RecvError> for crate::Error {
208 fn from(error: flume::RecvError) -> Self {
209 Self::Api(Error::InternalError(error.to_string()))
210 }
211}
212
213impl From<url::ParseError> for crate::Error {
214 fn from(error: url::ParseError) -> Self {
215 Self::Api(Error::InternalError(error.to_string()))
216 }
217}
218
219#[cfg(all(feature = "protocol-ws", target_arch = "wasm32"))]
220#[cfg_attr(docsrs, doc(cfg(all(feature = "protocol-ws", target_arch = "wasm32"))))]
221impl From<ws_stream_wasm::WsErr> for crate::Error {
222 fn from(error: ws_stream_wasm::WsErr) -> Self {
223 Self::Api(Error::Ws(error.to_string()))
224 }
225}
226
227#[cfg(all(feature = "protocol-ws", target_arch = "wasm32"))]
228#[cfg_attr(docsrs, doc(cfg(all(feature = "protocol-ws", target_arch = "wasm32"))))]
229impl From<pharos::PharErr> for crate::Error {
230 fn from(error: pharos::PharErr) -> Self {
231 Self::Api(Error::Ws(error.to_string()))
232 }
233}
234
235impl Serialize for Error {
236 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
237 where
238 S: serde::Serializer,
239 {
240 serializer.serialize_str(self.to_string().as_str())
241 }
242}