Skip to main content

surrealdb/api/err/
mod.rs

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/// An error originating from a remote SurrealDB database
12#[derive(Error, Debug)]
13#[non_exhaustive]
14pub enum Error {
15	/// There was an error processing the query
16	#[error("{0}")]
17	Query(String),
18
19	/// There was an error processing a remote HTTP request
20	#[error("There was an error processing a remote HTTP request: {0}")]
21	Http(String),
22
23	/// There was an error processing a remote WS request
24	#[error("There was an error processing a remote WS request: {0}")]
25	Ws(String),
26
27	/// The specified scheme does not match any supported protocol or storage
28	/// 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("Tried to add a range to an record-id resource")]
46	RangeOnRecordId,
47
48	/// Tried to use a range query on an object
49	#[error("Tried to add a range to an object resource")]
50	RangeOnObject,
51
52	/// Tried to use a range query on an array
53	#[error("Tried to add a range to an array resource")]
54	RangeOnArray,
55
56	/// Tried to use a range query on an edge or edges
57	#[error("Tried to add a range to an edge resource")]
58	RangeOnEdges,
59
60	/// Tried to use a range query on an existing range
61	#[error("Tried to add a range to a resource which was already a range")]
62	RangeOnRange,
63
64	/// Tried to use a range query on an unspecified resource
65	#[error("Tried to add a range to an unspecified resource")]
66	RangeOnUnspecified,
67
68	/// Tried to use `table:id` syntax as a method parameter when `(table, id)`
69	/// should be used instead
70	#[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	/// Duplicate request ID
78	#[error("Duplicate request ID: {0}")]
79	DuplicateRequestId(i64),
80
81	/// Invalid request
82	#[error("Invalid request: {0}")]
83	InvalidRequest(String),
84
85	/// Invalid params
86	#[error("Invalid params: {0}")]
87	InvalidParams(String),
88
89	/// Internal server error
90	#[error("Internal error: {0}")]
91	InternalError(String),
92
93	/// Parse error
94	#[error("Parse error: {0}")]
95	ParseError(String),
96
97	/// Invalid semantic version
98	#[error("Invalid semantic version: {0}")]
99	InvalidSemanticVersion(String),
100
101	/// Invalid URL
102	#[error("Invalid URL: {0}")]
103	InvalidUrl(String),
104
105	/// Failed to convert a `sql::Value` to `T`
106	#[error("Failed to convert `{value}` to `T`: {error}")]
107	FromValue {
108		value: Value,
109		error: String,
110	},
111
112	/// Failed to deserialize a binary response
113	#[error("Failed to deserialize a binary response: {error}")]
114	ResponseFromBinary {
115		binary: Vec<u8>,
116		error: bincode::Error,
117	},
118
119	/// Failed to serialize `sql::Value` to JSON string
120	#[error("Failed to serialize `{value}` to JSON string: {error}")]
121	ToJsonString {
122		value: Value,
123		error: String,
124	},
125
126	/// Failed to deserialize from JSON string to `sql::Value`
127	#[error("Failed to deserialize `{string}` to sql::Value: {error}")]
128	FromJsonString {
129		string: String,
130		error: String,
131	},
132
133	/// Invalid namespace name
134	#[error("Invalid namespace name: {0:?}")]
135	InvalidNsName(String),
136
137	/// Invalid database name
138	#[error("Invalid database name: {0:?}")]
139	InvalidDbName(String),
140
141	/// File open error
142	#[error("Failed to open `{path}`: {error}")]
143	FileOpen {
144		path: PathBuf,
145		error: io::Error,
146	},
147
148	/// File read error
149	#[error("Failed to read `{path}`: {error}")]
150	FileRead {
151		path: PathBuf,
152		error: io::Error,
153	},
154
155	/// Tried to take only a single result when the query returned multiple
156	/// records
157	#[error("Tried to take only a single result from a query that contains multiple")]
158	LossyTake(Response),
159
160	/// The protocol or storage engine being used does not support backups on
161	/// the architecture it's running on
162	#[error("The protocol or storage engine does not support backups on this architecture")]
163	BackupsNotSupported,
164
165	/// The version of the server is not compatible with the versions supported
166	/// by this SDK
167	#[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	/// The build metadata of the server is older than the minimum supported by
176	/// this SDK
177	#[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	/// The protocol or storage engine being used does not support live queries
186	/// on the architecture it's running on
187	#[error("The protocol or storage engine does not support live queries on this architecture")]
188	LiveQueriesNotSupported,
189
190	/// Tried to use a range query on an object
191	#[error("Live queries on objects not supported")]
192	LiveOnObject,
193
194	/// Tried to use a range query on an array
195	#[error("Live queries on arrays not supported")]
196	LiveOnArray,
197
198	/// Tried to use a range query on an edge or edges
199	#[error("Live queries on edges not supported")]
200	LiveOnEdges,
201
202	/// Tried to use a range query on an unspecified resource
203	#[error("Live queries on unspecified resource not supported")]
204	LiveOnUnspecified,
205
206	/// Tried to access a query statement as a live query when it isn't a live
207	/// query
208	#[error("Query statement {0} is not a live query")]
209	NotLiveQuery(usize),
210
211	/// Tried to access a query statement falling outside the bounds of the
212	/// statements supplied
213	#[error("Query statement {0} is out of bounds")]
214	QueryIndexOutOfBounds(usize),
215
216	/// Called `Response::take` or `Response::stream` on a query response more
217	/// than once
218	#[error("Tried to take a query response that has already been taken")]
219	ResponseAlreadyTaken,
220
221	/// Tried to insert on an object
222	#[error("Insert queries on objects are not supported")]
223	InsertOnObject,
224
225	/// Tried to insert on an array
226	#[error("Insert queries on arrays are not supported")]
227	InsertOnArray,
228
229	/// Tried to insert on an edge or edges
230	#[error("Insert queries on edges are not supported")]
231	InsertOnEdges,
232
233	/// Tried to insert on an edge or edges
234	#[error("Insert queries on ranges are not supported")]
235	InsertOnRange,
236
237	/// Tried to insert on an unspecified resource with no data
238	#[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	/// Tried to convert an value which contained something like for example a
267	/// query or future.
268	#[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	/// The engine used does not support data versioning
274	#[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}