1use std::error::Error;
2use std::sync::Arc;
3
4pub use super::request::{
5 auth_response::AuthResponseSerializationError,
6 batch::{BatchSerializationError, BatchStatementSerializationError},
7 execute::ExecuteSerializationError,
8 prepare::PrepareSerializationError,
9 query::{QueryParametersSerializationError, QuerySerializationError},
10 register::RegisterSerializationError,
11 startup::StartupSerializationError,
12};
13
14use super::response::CqlResponseKind;
15use super::TryFromPrimitiveError;
16use thiserror::Error;
17
18#[derive(Error, Debug, Clone)]
30#[non_exhaustive]
31pub enum FrameBodyExtensionsParseError {
32 #[error("Frame is compressed, but no compression negotiated for connection.")]
34 NoCompressionNegotiated,
35
36 #[error("Malformed trace id: {0}")]
38 TraceIdParse(LowLevelDeserializationError),
39
40 #[error("Malformed warnings list: {0}")]
42 WarningsListParse(LowLevelDeserializationError),
43
44 #[error("Malformed custom payload map: {0}")]
46 CustomPayloadMapParse(LowLevelDeserializationError),
47
48 #[error("Snap decompression error: {0}")]
50 SnapDecompressError(Arc<dyn Error + Sync + Send>),
51
52 #[error("Error decompressing lz4 data {0}")]
54 Lz4DecompressError(Arc<dyn Error + Sync + Send>),
55}
56
57#[derive(Debug, Error)]
59#[non_exhaustive]
60pub enum FrameHeaderParseError {
61 #[error("Failed to read the frame header: {0}")]
63 HeaderIoError(std::io::Error),
64
65 #[error("Received frame marked as coming from a client")]
67 FrameFromClient,
68
69 #[error("Received frame marked as coming from the server")]
72 FrameFromServer,
73
74 #[error("Received a frame from version {0}, but only 4 is supported")]
76 VersionNotSupported(u8),
77
78 #[error("Unrecognized response opcode {0}")]
80 UnknownResponseOpcode(#[from] TryFromPrimitiveError<u8>),
81
82 #[error("Failed to read a chunk of response body. Expected {0} more bytes, error: {1}")]
84 BodyChunkIoError(usize, std::io::Error),
85
86 #[error("Connection was closed before body was read: missing {0} out of {1}")]
88 ConnectionClosed(usize, usize),
89}
90
91#[non_exhaustive]
93#[derive(Error, Debug, Clone)]
94pub enum CqlRequestSerializationError {
95 #[error("Failed to serialize STARTUP request: {0}")]
97 StartupSerialization(#[from] StartupSerializationError),
98
99 #[error("Failed to serialize REGISTER request: {0}")]
101 RegisterSerialization(#[from] RegisterSerializationError),
102
103 #[error("Failed to serialize AUTH_RESPONSE request: {0}")]
105 AuthResponseSerialization(#[from] AuthResponseSerializationError),
106
107 #[error("Failed to serialize BATCH request: {0}")]
109 BatchSerialization(#[from] BatchSerializationError),
110
111 #[error("Failed to serialize PREPARE request: {0}")]
113 PrepareSerialization(#[from] PrepareSerializationError),
114
115 #[error("Failed to serialize EXECUTE request: {0}")]
117 ExecuteSerialization(#[from] ExecuteSerializationError),
118
119 #[error("Failed to serialize QUERY request: {0}")]
121 QuerySerialization(#[from] QuerySerializationError),
122
123 #[error("Snap compression error: {0}")]
125 SnapCompressError(Arc<dyn Error + Sync + Send>),
126}
127
128#[non_exhaustive]
131#[derive(Error, Debug, Clone)]
132pub enum CqlResponseParseError {
133 #[error("Failed to deserialize ERROR response: {0}")]
134 CqlErrorParseError(#[from] CqlErrorParseError),
135 #[error("Failed to deserialize AUTH_CHALLENGE response: {0}")]
136 CqlAuthChallengeParseError(#[from] CqlAuthChallengeParseError),
137 #[error("Failed to deserialize AUTH_SUCCESS response: {0}")]
138 CqlAuthSuccessParseError(#[from] CqlAuthSuccessParseError),
139 #[error("Failed to deserialize AUTHENTICATE response: {0}")]
140 CqlAuthenticateParseError(#[from] CqlAuthenticateParseError),
141 #[error("Failed to deserialize SUPPORTED response: {0}")]
142 CqlSupportedParseError(#[from] CqlSupportedParseError),
143 #[error("Failed to deserialize EVENT response: {0}")]
144 CqlEventParseError(#[from] CqlEventParseError),
145 #[error(transparent)]
146 CqlResultParseError(#[from] CqlResultParseError),
147}
148
149impl CqlResponseParseError {
150 pub fn to_response_kind(&self) -> CqlResponseKind {
151 match self {
152 CqlResponseParseError::CqlErrorParseError(_) => CqlResponseKind::Error,
153 CqlResponseParseError::CqlAuthChallengeParseError(_) => CqlResponseKind::AuthChallenge,
154 CqlResponseParseError::CqlAuthSuccessParseError(_) => CqlResponseKind::AuthSuccess,
155 CqlResponseParseError::CqlAuthenticateParseError(_) => CqlResponseKind::Authenticate,
156 CqlResponseParseError::CqlSupportedParseError(_) => CqlResponseKind::Supported,
157 CqlResponseParseError::CqlEventParseError(_) => CqlResponseKind::Event,
158 CqlResponseParseError::CqlResultParseError(_) => CqlResponseKind::Result,
159 }
160 }
161}
162
163#[non_exhaustive]
165#[derive(Error, Debug, Clone)]
166pub enum CqlErrorParseError {
167 #[error("Malformed error code: {0}")]
168 ErrorCodeParseError(LowLevelDeserializationError),
169 #[error("Malformed error reason: {0}")]
170 ReasonParseError(LowLevelDeserializationError),
171 #[error("Malformed error field {field} of DB error {db_error}: {err}")]
172 MalformedErrorField {
173 db_error: &'static str,
174 field: &'static str,
175 err: LowLevelDeserializationError,
176 },
177}
178
179#[non_exhaustive]
181#[derive(Error, Debug, Clone)]
182pub enum CqlAuthChallengeParseError {
183 #[error("Malformed authenticate message: {0}")]
184 AuthMessageParseError(LowLevelDeserializationError),
185}
186
187#[non_exhaustive]
189#[derive(Error, Debug, Clone)]
190pub enum CqlAuthSuccessParseError {
191 #[error("Malformed success message: {0}")]
192 SuccessMessageParseError(LowLevelDeserializationError),
193}
194
195#[non_exhaustive]
197#[derive(Error, Debug, Clone)]
198pub enum CqlAuthenticateParseError {
199 #[error("Malformed authenticator name: {0}")]
200 AuthNameParseError(LowLevelDeserializationError),
201}
202
203#[non_exhaustive]
205#[derive(Error, Debug, Clone)]
206pub enum CqlSupportedParseError {
207 #[error("Malformed options map: {0}")]
208 OptionsMapDeserialization(LowLevelDeserializationError),
209}
210
211#[non_exhaustive]
213#[derive(Error, Debug, Clone)]
214pub enum CqlResultParseError {
215 #[error("Malformed RESULT response id: {0}")]
216 ResultIdParseError(LowLevelDeserializationError),
217 #[error("Unknown RESULT response id: {0}")]
218 UnknownResultId(i32),
219 #[error("RESULT:Set_keyspace response deserialization failed: {0}")]
220 SetKeyspaceParseError(#[from] SetKeyspaceParseError),
221 #[error("RESULT:Schema_change response deserialization failed: {0}")]
224 SchemaChangeParseError(#[from] SchemaChangeEventParseError),
225 #[error("RESULT:Prepared response deserialization failed: {0}")]
226 PreparedParseError(#[from] PreparedParseError),
227 #[error("RESULT:Rows response deserialization failed: {0}")]
228 RawRowsParseError(#[from] RawRowsAndPagingStateResponseParseError),
229}
230
231#[non_exhaustive]
232#[derive(Error, Debug, Clone)]
233pub enum SetKeyspaceParseError {
234 #[error("Malformed keyspace name: {0}")]
235 MalformedKeyspaceName(#[from] LowLevelDeserializationError),
236}
237
238#[non_exhaustive]
241#[derive(Error, Debug, Clone)]
242pub enum CqlEventParseError {
243 #[error("Malformed event type string: {0}")]
244 EventTypeParseError(LowLevelDeserializationError),
245 #[error("Unknown event type: {0}")]
246 UnknownEventType(String),
247 #[error("Failed to deserialize schema change event: {0}")]
248 SchemaChangeEventParseError(#[from] SchemaChangeEventParseError),
249 #[error("Failed to deserialize topology change event: {0}")]
250 TopologyChangeEventParseError(ClusterChangeEventParseError),
251 #[error("Failed to deserialize status change event: {0}")]
252 StatusChangeEventParseError(ClusterChangeEventParseError),
253}
254
255#[non_exhaustive]
258#[derive(Error, Debug, Clone)]
259pub enum SchemaChangeEventParseError {
260 #[error("Malformed schema change type string: {0}")]
261 TypeOfChangeParseError(LowLevelDeserializationError),
262 #[error("Malformed schema change target string:: {0}")]
263 TargetTypeParseError(LowLevelDeserializationError),
264 #[error("Malformed name of keyspace affected by schema change: {0}")]
265 AffectedKeyspaceParseError(LowLevelDeserializationError),
266 #[error("Malformed name of the table affected by schema change: {0}")]
267 AffectedTableNameParseError(LowLevelDeserializationError),
268 #[error("Malformed name of the target affected by schema change: {0}")]
269 AffectedTargetNameParseError(LowLevelDeserializationError),
270 #[error(
271 "Malformed number of arguments of the function/aggregate affected by schema change: {0}"
272 )]
273 ArgumentCountParseError(LowLevelDeserializationError),
274 #[error("Malformed argument of the function/aggregate affected by schema change: {0}")]
275 FunctionArgumentParseError(LowLevelDeserializationError),
276 #[error("Unknown target of schema change: {0}")]
277 UnknownTargetOfSchemaChange(String),
278}
279
280#[non_exhaustive]
282#[derive(Error, Debug, Clone)]
283pub enum ClusterChangeEventParseError {
284 #[error("Malformed type of change: {0}")]
285 TypeOfChangeParseError(LowLevelDeserializationError),
286 #[error("Malformed node address: {0}")]
287 NodeAddressParseError(LowLevelDeserializationError),
288 #[error("Unknown type of change: {0}")]
289 UnknownTypeOfChange(String),
290}
291
292#[non_exhaustive]
295#[derive(Debug, Error, Clone)]
296pub enum PreparedParseError {
297 #[error("Malformed prepared statement's id length: {0}")]
298 IdLengthParseError(LowLevelDeserializationError),
299 #[error("Invalid result metadata: {0}")]
300 ResultMetadataParseError(ResultMetadataParseError),
301 #[error("Invalid prepared metadata: {0}")]
302 PreparedMetadataParseError(PreparedMetadataParseError),
303 #[error("Non-zero paging state in result metadata: {0:?}")]
304 NonZeroPagingState(Arc<[u8]>),
305}
306
307#[non_exhaustive]
314#[derive(Debug, Error, Clone)]
315pub enum RawRowsAndPagingStateResponseParseError {
316 #[error("Malformed metadata flags: {0}")]
318 FlagsParseError(LowLevelDeserializationError),
319
320 #[error("Malformed column count: {0}")]
322 ColumnCountParseError(LowLevelDeserializationError),
323
324 #[error("Malformed paging state: {0}")]
326 PagingStateParseError(LowLevelDeserializationError),
327}
328
329#[non_exhaustive]
332#[derive(Error, Debug, Clone)]
333pub enum PreparedMetadataParseError {
334 #[error("Malformed metadata flags: {0}")]
336 FlagsParseError(LowLevelDeserializationError),
337
338 #[error("Malformed column count: {0}")]
340 ColumnCountParseError(LowLevelDeserializationError),
341
342 #[error("Malformed partition key count: {0}")]
344 PkCountParseError(LowLevelDeserializationError),
345
346 #[error("Malformed partition key index: {0}")]
348 PkIndexParseError(LowLevelDeserializationError),
349
350 #[error("Invalid global table spec: {0}")]
352 GlobalTableSpecParseError(#[from] TableSpecParseError),
353
354 #[error("Invalid column spec: {0}")]
356 ColumnSpecParseError(#[from] ColumnSpecParseError),
357}
358
359#[non_exhaustive]
362#[derive(Error, Debug, Clone)]
363pub enum ResultMetadataAndRowsCountParseError {
364 #[error("Failed to lazily deserialize result metadata: {0}")]
366 ResultMetadataParseError(#[from] ResultMetadataParseError),
367
368 #[error("Malformed rows count: {0}")]
370 RowsCountParseError(LowLevelDeserializationError),
371}
372
373#[non_exhaustive]
376#[derive(Error, Debug, Clone)]
377pub enum ResultMetadataParseError {
378 #[error("Malformed metadata flags: {0}")]
380 FlagsParseError(LowLevelDeserializationError),
381
382 #[error("Malformed column count: {0}")]
384 ColumnCountParseError(LowLevelDeserializationError),
385
386 #[error("Malformed paging state: {0}")]
388 PagingStateParseError(LowLevelDeserializationError),
389
390 #[error("Invalid global table spec: {0}")]
392 GlobalTableSpecParseError(#[from] TableSpecParseError),
393
394 #[error("Invalid column spec: {0}")]
396 ColumnSpecParseError(#[from] ColumnSpecParseError),
397}
398
399#[non_exhaustive]
402#[derive(Error, Debug, Clone)]
403pub enum TableSpecParseError {
404 #[error("Malformed keyspace name: {0}")]
405 MalformedKeyspaceName(LowLevelDeserializationError),
406 #[error("Malformed table name: {0}")]
407 MalformedTableName(LowLevelDeserializationError),
408}
409
410#[non_exhaustive]
413#[derive(Error, Debug, Clone)]
414#[error("Column spec deserialization failed, column index: {column_index}, error: {kind}")]
415pub struct ColumnSpecParseError {
416 pub column_index: usize,
417 pub kind: ColumnSpecParseErrorKind,
418}
419
420#[non_exhaustive]
423#[derive(Error, Debug, Clone)]
424pub enum ColumnSpecParseErrorKind {
425 #[error("Invalid table spec: {0}")]
426 TableSpecParseError(#[from] TableSpecParseError),
427 #[error("Malformed column name: {0}")]
428 ColumnNameParseError(#[from] LowLevelDeserializationError),
429 #[error("Invalid column type: {0}")]
430 ColumnTypeParseError(#[from] CqlTypeParseError),
431}
432
433#[non_exhaustive]
435#[derive(Error, Debug, Clone)]
436pub enum CqlTypeParseError {
437 #[error("Malformed type id: {0}")]
438 TypeIdParseError(LowLevelDeserializationError),
439 #[error("Malformed custom type name: {0}")]
440 CustomTypeNameParseError(LowLevelDeserializationError),
441 #[error("Unsupported custom type: {0}")]
442 CustomTypeUnsupported(String),
443 #[error("Malformed name of UDT keyspace: {0}")]
444 UdtKeyspaceNameParseError(LowLevelDeserializationError),
445 #[error("Malformed UDT name: {0}")]
446 UdtNameParseError(LowLevelDeserializationError),
447 #[error("Malformed UDT fields count: {0}")]
448 UdtFieldsCountParseError(LowLevelDeserializationError),
449 #[error("Malformed UDT's field name: {0}")]
450 UdtFieldNameParseError(LowLevelDeserializationError),
451 #[error("Malformed tuple length: {0}")]
452 TupleLengthParseError(LowLevelDeserializationError),
453 #[error("CQL Type not yet implemented, id: {0}")]
454 TypeNotImplemented(u16),
455}
456
457#[non_exhaustive]
468#[derive(Error, Debug, Clone)]
469pub enum LowLevelDeserializationError {
470 #[error(transparent)]
471 IoError(Arc<std::io::Error>),
472 #[error(transparent)]
473 TryFromIntError(#[from] std::num::TryFromIntError),
474 #[error(transparent)]
475 TryFromSliceError(#[from] std::array::TryFromSliceError),
476 #[error("Not enough bytes! expected: {expected}, received: {received}")]
477 TooFewBytesReceived { expected: usize, received: usize },
478 #[error("Invalid value length: {0}")]
479 InvalidValueLength(i32),
480 #[error("Unknown consistency: {0}")]
481 UnknownConsistency(#[from] TryFromPrimitiveError<u16>),
482 #[error("Invalid inet bytes length: {0}. Accepted lengths are 4 and 16 bytes.")]
483 InvalidInetLength(u8),
484 #[error("UTF8 deserialization failed: {0}")]
485 UTF8DeserializationError(#[from] std::str::Utf8Error),
486}
487
488impl From<std::io::Error> for LowLevelDeserializationError {
489 fn from(value: std::io::Error) -> Self {
490 Self::IoError(Arc::new(value))
491 }
492}