surrealdb/api/err/
mod.rs

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/// An error originating from a remote SurrealDB database
13#[derive(Error, Debug)]
14#[non_exhaustive]
15pub enum Error {
16	/// There was an error processing the query
17	#[error("{0}")]
18	Query(String),
19
20	/// There was an error processing a remote HTTP request
21	#[error("There was an error processing a remote HTTP request: {0}")]
22	Http(String),
23
24	/// There was an error processing a remote WS request
25	#[error("There was an error processing a remote WS request: {0}")]
26	Ws(String),
27
28	/// The specified scheme does not match any supported protocol or storage engine
29	#[error("Unsupported protocol or storage engine, `{0}`")]
30	Scheme(String),
31
32	/// Tried to run database queries without initialising the connection first
33	#[error("Connection uninitialised")]
34	ConnectionUninitialised,
35
36	/// Tried to call `connect` on an instance already connected
37	#[error("Already connected")]
38	AlreadyConnected,
39
40	/// `Query::bind` not called with an object nor a key/value tuple
41	#[error("Invalid bindings: {0}")]
42	InvalidBindings(Value),
43
44	/// Tried to use a range query on a record ID
45	#[error("Range on record IDs not supported: {0}")]
46	RangeOnRecordId(Thing),
47
48	/// Tried to use a range query on an object
49	#[error("Range on objects not supported: {0}")]
50	RangeOnObject(Object),
51
52	/// Tried to use a range query on an array
53	#[error("Range on arrays not supported: {0}")]
54	RangeOnArray(Array),
55
56	/// Tried to use a range query on an edge or edges
57	#[error("Range on edges not supported: {0}")]
58	RangeOnEdges(Edges),
59
60	/// Tried to use `table:id` syntax as a method parameter when `(table, id)` should be used instead
61	#[error("`{table}:{id}` is not allowed as a method parameter; try `({table}, {id})`")]
62	TableColonId {
63		table: String,
64		id: String,
65	},
66
67	/// Duplicate request ID
68	#[error("Duplicate request ID: {0}")]
69	DuplicateRequestId(i64),
70
71	/// Invalid request
72	#[error("Invalid request: {0}")]
73	InvalidRequest(String),
74
75	/// Invalid params
76	#[error("Invalid params: {0}")]
77	InvalidParams(String),
78
79	/// Internal server error
80	#[error("Internal error: {0}")]
81	InternalError(String),
82
83	/// Parse error
84	#[error("Parse error: {0}")]
85	ParseError(String),
86
87	/// Invalid semantic version
88	#[error("Invalid semantic version: {0}")]
89	InvalidSemanticVersion(String),
90
91	/// Invalid URL
92	#[error("Invalid URL: {0}")]
93	InvalidUrl(String),
94
95	/// Failed to convert a `sql::Value` to `T`
96	#[error("Failed to convert `{value}` to `T`: {error}")]
97	FromValue {
98		value: Value,
99		error: String,
100	},
101
102	/// Failed to deserialize a binary response
103	#[error("Failed to deserialize a binary response: {error}")]
104	ResponseFromBinary {
105		binary: Vec<u8>,
106		error: bincode::Error,
107	},
108
109	/// Failed to serialize `sql::Value` to JSON string
110	#[error("Failed to serialize `{value}` to JSON string: {error}")]
111	ToJsonString {
112		value: Value,
113		error: String,
114	},
115
116	/// Failed to deserialize from JSON string to `sql::Value`
117	#[error("Failed to deserialize `{string}` to sql::Value: {error}")]
118	FromJsonString {
119		string: String,
120		error: String,
121	},
122
123	/// Invalid namespace name
124	#[error("Invalid namespace name: {0:?}")]
125	InvalidNsName(String),
126
127	/// Invalid database name
128	#[error("Invalid database name: {0:?}")]
129	InvalidDbName(String),
130
131	/// File open error
132	#[error("Failed to open `{path}`: {error}")]
133	FileOpen {
134		path: PathBuf,
135		error: io::Error,
136	},
137
138	/// File read error
139	#[error("Failed to read `{path}`: {error}")]
140	FileRead {
141		path: PathBuf,
142		error: io::Error,
143	},
144
145	/// Tried to take only a single result when the query returned multiple records
146	#[error("Tried to take only a single result from a query that contains multiple")]
147	LossyTake(Response),
148
149	/// The protocol or storage engine being used does not support backups on the architecture
150	/// it's running on
151	#[error("The protocol or storage engine does not support backups on this architecture")]
152	BackupsNotSupported,
153
154	/// The version of the server is not compatible with the versions supported by this SDK
155	#[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	/// The build metadata of the server is older than the minimum supported by this SDK
162	#[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	/// The protocol or storage engine being used does not support live queries on the architecture
169	/// it's running on
170	#[error("The protocol or storage engine does not support live queries on this architecture")]
171	LiveQueriesNotSupported,
172
173	/// Tried to use a range query on an object
174	#[error("Live queries on objects not supported: {0}")]
175	LiveOnObject(Object),
176
177	/// Tried to use a range query on an array
178	#[error("Live queries on arrays not supported: {0}")]
179	LiveOnArray(Array),
180
181	/// Tried to use a range query on an edge or edges
182	#[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}