surrealdb/api/err/
mod.rs

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