surrealdb/api/err/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use crate::api::Response;
use crate::sql::Array;
use crate::sql::Edges;
use crate::sql::FromValueError;
use crate::sql::Object;
use crate::sql::Thing;
use crate::sql::Value;
use serde::Serialize;
use std::io;
use std::path::PathBuf;
use thiserror::Error;

/// An error originating from a remote SurrealDB database
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
	/// There was an error processing the query
	#[error("{0}")]
	Query(String),

	/// There was an error processing a remote HTTP request
	#[error("There was an error processing a remote HTTP request: {0}")]
	Http(String),

	/// There was an error processing a remote WS request
	#[error("There was an error processing a remote WS request: {0}")]
	Ws(String),

	/// The specified scheme does not match any supported protocol or storage engine
	#[error("Unsupported protocol or storage engine, `{0}`")]
	Scheme(String),

	/// Tried to run database queries without initialising the connection first
	#[error("Connection uninitialised")]
	ConnectionUninitialised,

	/// Tried to call `connect` on an instance already connected
	#[error("Already connected")]
	AlreadyConnected,

	/// `Query::bind` not called with an object nor a key/value tuple
	#[error("Invalid bindings: {0}")]
	InvalidBindings(Value),

	/// Tried to use a range query on a record ID
	#[error("Range on record IDs not supported: {0}")]
	RangeOnRecordId(Thing),

	/// Tried to use a range query on an object
	#[error("Range on objects not supported: {0}")]
	RangeOnObject(Object),

	/// Tried to use a range query on an array
	#[error("Range on arrays not supported: {0}")]
	RangeOnArray(Array),

	/// Tried to use a range query on an edge or edges
	#[error("Range on edges not supported: {0}")]
	RangeOnEdges(Edges),

	/// Tried to use `table:id` syntax as a method parameter when `(table, id)` should be used instead
	#[error("`{table}:{id}` is not allowed as a method parameter; try `({table}, {id})`")]
	TableColonId {
		table: String,
		id: String,
	},

	/// Duplicate request ID
	#[error("Duplicate request ID: {0}")]
	DuplicateRequestId(i64),

	/// Invalid request
	#[error("Invalid request: {0}")]
	InvalidRequest(String),

	/// Invalid params
	#[error("Invalid params: {0}")]
	InvalidParams(String),

	/// Internal server error
	#[error("Internal error: {0}")]
	InternalError(String),

	/// Parse error
	#[error("Parse error: {0}")]
	ParseError(String),

	/// Invalid semantic version
	#[error("Invalid semantic version: {0}")]
	InvalidSemanticVersion(String),

	/// Invalid URL
	#[error("Invalid URL: {0}")]
	InvalidUrl(String),

	/// Failed to convert a `sql::Value` to `T`
	#[error("Failed to convert `{value}` to `T`: {error}")]
	FromValue {
		value: Value,
		error: String,
	},

	/// Failed to deserialize a binary response
	#[error("Failed to deserialize a binary response: {error}")]
	ResponseFromBinary {
		binary: Vec<u8>,
		error: bincode::Error,
	},

	/// Failed to serialize `sql::Value` to JSON string
	#[error("Failed to serialize `{value}` to JSON string: {error}")]
	ToJsonString {
		value: Value,
		error: String,
	},

	/// Failed to deserialize from JSON string to `sql::Value`
	#[error("Failed to deserialize `{string}` to sql::Value: {error}")]
	FromJsonString {
		string: String,
		error: String,
	},

	/// Invalid namespace name
	#[error("Invalid namespace name: {0:?}")]
	InvalidNsName(String),

	/// Invalid database name
	#[error("Invalid database name: {0:?}")]
	InvalidDbName(String),

	/// File open error
	#[error("Failed to open `{path}`: {error}")]
	FileOpen {
		path: PathBuf,
		error: io::Error,
	},

	/// File read error
	#[error("Failed to read `{path}`: {error}")]
	FileRead {
		path: PathBuf,
		error: io::Error,
	},

	/// Tried to take only a single result when the query returned multiple records
	#[error("Tried to take only a single result from a query that contains multiple")]
	LossyTake(Response),

	/// The protocol or storage engine being used does not support backups on the architecture
	/// it's running on
	#[error("The protocol or storage engine does not support backups on this architecture")]
	BackupsNotSupported,

	/// The version of the server is not compatible with the versions supported by this SDK
	#[error("server version `{server_version}` does not match the range supported by the client `{supported_versions}`")]
	VersionMismatch {
		server_version: semver::Version,
		supported_versions: String,
	},

	/// The build metadata of the server is older than the minimum supported by this SDK
	#[error("server build `{server_metadata}` is older than the minimum supported build `{supported_metadata}`")]
	BuildMetadataMismatch {
		server_metadata: semver::BuildMetadata,
		supported_metadata: semver::BuildMetadata,
	},

	/// The protocol or storage engine being used does not support live queries on the architecture
	/// it's running on
	#[error("The protocol or storage engine does not support live queries on this architecture")]
	LiveQueriesNotSupported,

	/// Tried to use a range query on an object
	#[error("Live queries on objects not supported: {0}")]
	LiveOnObject(Object),

	/// Tried to use a range query on an array
	#[error("Live queries on arrays not supported: {0}")]
	LiveOnArray(Array),

	/// Tried to use a range query on an edge or edges
	#[error("Live queries on edges not supported: {0}")]
	LiveOnEdges(Edges),

	/// Tried to access a query statement as a live query when it isn't a live query
	#[error("Query statement {0} is not a live query")]
	NotLiveQuery(usize),

	/// Tried to access a query statement falling outside the bounds of the statements supplied
	#[error("Query statement {0} is out of bounds")]
	QueryIndexOutOfBounds(usize),

	/// Called `Response::take` or `Response::stream` on a query response more than once
	#[error("Tried to take a query response that has already been taken")]
	ResponseAlreadyTaken,

	/// Tried to insert on an object
	#[error("Insert queries on objects not supported: {0}")]
	InsertOnObject(Object),

	/// Tried to insert on an array
	#[error("Insert queries on arrays not supported: {0}")]
	InsertOnArray(Array),

	/// Tried to insert on an edge or edges
	#[error("Insert queries on edges not supported: {0}")]
	InsertOnEdges(Edges),
}

#[cfg(feature = "protocol-http")]
impl From<reqwest::Error> for crate::Error {
	fn from(e: reqwest::Error) -> Self {
		Self::Api(Error::Http(e.to_string()))
	}
}

#[cfg(all(feature = "protocol-ws", not(target_arch = "wasm32")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "protocol-ws", not(target_arch = "wasm32")))))]
impl From<tokio_tungstenite::tungstenite::Error> for crate::Error {
	fn from(error: tokio_tungstenite::tungstenite::Error) -> Self {
		Self::Api(Error::Ws(error.to_string()))
	}
}

impl<T> From<flume::SendError<T>> for crate::Error {
	fn from(error: flume::SendError<T>) -> Self {
		Self::Api(Error::InternalError(error.to_string()))
	}
}

impl From<flume::RecvError> for crate::Error {
	fn from(error: flume::RecvError) -> Self {
		Self::Api(Error::InternalError(error.to_string()))
	}
}

impl From<url::ParseError> for crate::Error {
	fn from(error: url::ParseError) -> Self {
		Self::Api(Error::InternalError(error.to_string()))
	}
}

#[cfg(all(feature = "protocol-ws", target_arch = "wasm32"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "protocol-ws", target_arch = "wasm32"))))]
impl From<ws_stream_wasm::WsErr> for crate::Error {
	fn from(error: ws_stream_wasm::WsErr) -> Self {
		Self::Api(Error::Ws(error.to_string()))
	}
}

#[cfg(all(feature = "protocol-ws", target_arch = "wasm32"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "protocol-ws", target_arch = "wasm32"))))]
impl From<pharos::PharErr> for crate::Error {
	fn from(error: pharos::PharErr) -> Self {
		Self::Api(Error::Ws(error.to_string()))
	}
}

impl Serialize for Error {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: serde::Serializer,
	{
		serializer.serialize_str(self.to_string().as_str())
	}
}

impl From<FromValueError> for crate::Error {
	fn from(error: FromValueError) -> Self {
		Self::Api(Error::FromValue {
			value: error.value,
			error: error.error,
		})
	}
}