use std::error::Error;
use std::sync::Arc;
pub use super::request::{
auth_response::AuthResponseSerializationError,
batch::{BatchSerializationError, BatchStatementSerializationError},
execute::ExecuteSerializationError,
prepare::PrepareSerializationError,
query::{QueryParametersSerializationError, QuerySerializationError},
register::RegisterSerializationError,
startup::StartupSerializationError,
};
use super::response::result::TableSpec;
use super::response::CqlResponseKind;
use super::TryFromPrimitiveError;
use thiserror::Error;
#[derive(Error, Debug, Clone)]
#[non_exhaustive]
pub enum FrameBodyExtensionsParseError {
#[error("Frame is compressed, but no compression negotiated for connection.")]
NoCompressionNegotiated,
#[error("Malformed trace id: {0}")]
TraceIdParse(LowLevelDeserializationError),
#[error("Malformed warnings list: {0}")]
WarningsListParse(LowLevelDeserializationError),
#[error("Malformed custom payload map: {0}")]
CustomPayloadMapParse(LowLevelDeserializationError),
#[error("Snap decompression error: {0}")]
SnapDecompressError(Arc<dyn Error + Sync + Send>),
#[error("Error decompressing lz4 data {0}")]
Lz4DecompressError(Arc<dyn Error + Sync + Send>),
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum FrameHeaderParseError {
#[error("Failed to read the frame header: {0}")]
HeaderIoError(std::io::Error),
#[error("Received frame marked as coming from a client")]
FrameFromClient,
#[error("Received frame marked as coming from the server")]
FrameFromServer,
#[error("Received a frame from version {0}, but only 4 is supported")]
VersionNotSupported(u8),
#[error("Unrecognized response opcode {0}")]
UnknownResponseOpcode(#[from] TryFromPrimitiveError<u8>),
#[error("Failed to read a chunk of response body. Expected {0} more bytes, error: {1}")]
BodyChunkIoError(usize, std::io::Error),
#[error("Connection was closed before body was read: missing {0} out of {1}")]
ConnectionClosed(usize, usize),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum CqlRequestSerializationError {
#[error("Failed to serialize STARTUP request: {0}")]
StartupSerialization(#[from] StartupSerializationError),
#[error("Failed to serialize REGISTER request: {0}")]
RegisterSerialization(#[from] RegisterSerializationError),
#[error("Failed to serialize AUTH_RESPONSE request: {0}")]
AuthResponseSerialization(#[from] AuthResponseSerializationError),
#[error("Failed to serialize BATCH request: {0}")]
BatchSerialization(#[from] BatchSerializationError),
#[error("Failed to serialize PREPARE request: {0}")]
PrepareSerialization(#[from] PrepareSerializationError),
#[error("Failed to serialize EXECUTE request: {0}")]
ExecuteSerialization(#[from] ExecuteSerializationError),
#[error("Failed to serialize QUERY request: {0}")]
QuerySerialization(#[from] QuerySerializationError),
#[error("Snap compression error: {0}")]
SnapCompressError(Arc<dyn Error + Sync + Send>),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum CqlResponseParseError {
#[error("Failed to deserialize ERROR response: {0}")]
CqlErrorParseError(#[from] CqlErrorParseError),
#[error("Failed to deserialize AUTH_CHALLENGE response: {0}")]
CqlAuthChallengeParseError(#[from] CqlAuthChallengeParseError),
#[error("Failed to deserialize AUTH_SUCCESS response: {0}")]
CqlAuthSuccessParseError(#[from] CqlAuthSuccessParseError),
#[error("Failed to deserialize AUTHENTICATE response: {0}")]
CqlAuthenticateParseError(#[from] CqlAuthenticateParseError),
#[error("Failed to deserialize SUPPORTED response: {0}")]
CqlSupportedParseError(#[from] CqlSupportedParseError),
#[error("Failed to deserialize EVENT response: {0}")]
CqlEventParseError(#[from] CqlEventParseError),
#[error(transparent)]
CqlResultParseError(#[from] CqlResultParseError),
}
impl CqlResponseParseError {
pub fn to_response_kind(&self) -> CqlResponseKind {
match self {
CqlResponseParseError::CqlErrorParseError(_) => CqlResponseKind::Error,
CqlResponseParseError::CqlAuthChallengeParseError(_) => CqlResponseKind::AuthChallenge,
CqlResponseParseError::CqlAuthSuccessParseError(_) => CqlResponseKind::AuthSuccess,
CqlResponseParseError::CqlAuthenticateParseError(_) => CqlResponseKind::Authenticate,
CqlResponseParseError::CqlSupportedParseError(_) => CqlResponseKind::Supported,
CqlResponseParseError::CqlEventParseError(_) => CqlResponseKind::Event,
CqlResponseParseError::CqlResultParseError(_) => CqlResponseKind::Result,
}
}
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum CqlErrorParseError {
#[error("Malformed error code: {0}")]
ErrorCodeParseError(LowLevelDeserializationError),
#[error("Malformed error reason: {0}")]
ReasonParseError(LowLevelDeserializationError),
#[error("Malformed error field {field} of DB error {db_error}: {err}")]
MalformedErrorField {
db_error: &'static str,
field: &'static str,
err: LowLevelDeserializationError,
},
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum CqlAuthChallengeParseError {
#[error("Malformed authenticate message: {0}")]
AuthMessageParseError(LowLevelDeserializationError),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum CqlAuthSuccessParseError {
#[error("Malformed success message: {0}")]
SuccessMessageParseError(LowLevelDeserializationError),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum CqlAuthenticateParseError {
#[error("Malformed authenticator name: {0}")]
AuthNameParseError(LowLevelDeserializationError),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum CqlSupportedParseError {
#[error("Malformed options map: {0}")]
OptionsMapDeserialization(LowLevelDeserializationError),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum CqlResultParseError {
#[error("Malformed RESULT response id: {0}")]
ResultIdParseError(LowLevelDeserializationError),
#[error("Unknown RESULT response id: {0}")]
UnknownResultId(i32),
#[error("RESULT:Set_keyspace response deserialization failed: {0}")]
SetKeyspaceParseError(#[from] SetKeyspaceParseError),
#[error("RESULT:Schema_change response deserialization failed: {0}")]
SchemaChangeParseError(#[from] SchemaChangeEventParseError),
#[error("RESULT:Prepared response deserialization failed: {0}")]
PreparedParseError(#[from] PreparedParseError),
#[error("RESULT:Rows response deserialization failed: {0}")]
RawRowsParseError(#[from] RawRowsAndPagingStateResponseParseError),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum SetKeyspaceParseError {
#[error("Malformed keyspace name: {0}")]
MalformedKeyspaceName(#[from] LowLevelDeserializationError),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum CqlEventParseError {
#[error("Malformed event type string: {0}")]
EventTypeParseError(LowLevelDeserializationError),
#[error("Unknown event type: {0}")]
UnknownEventType(String),
#[error("Failed to deserialize schema change event: {0}")]
SchemaChangeEventParseError(#[from] SchemaChangeEventParseError),
#[error("Failed to deserialize topology change event: {0}")]
TopologyChangeEventParseError(ClusterChangeEventParseError),
#[error("Failed to deserialize status change event: {0}")]
StatusChangeEventParseError(ClusterChangeEventParseError),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum SchemaChangeEventParseError {
#[error("Malformed schema change type string: {0}")]
TypeOfChangeParseError(LowLevelDeserializationError),
#[error("Malformed schema change target string:: {0}")]
TargetTypeParseError(LowLevelDeserializationError),
#[error("Malformed name of keyspace affected by schema change: {0}")]
AffectedKeyspaceParseError(LowLevelDeserializationError),
#[error("Malformed name of the table affected by schema change: {0}")]
AffectedTableNameParseError(LowLevelDeserializationError),
#[error("Malformed name of the target affected by schema change: {0}")]
AffectedTargetNameParseError(LowLevelDeserializationError),
#[error(
"Malformed number of arguments of the function/aggregate affected by schema change: {0}"
)]
ArgumentCountParseError(LowLevelDeserializationError),
#[error("Malformed argument of the function/aggregate affected by schema change: {0}")]
FunctionArgumentParseError(LowLevelDeserializationError),
#[error("Unknown target of schema change: {0}")]
UnknownTargetOfSchemaChange(String),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum ClusterChangeEventParseError {
#[error("Malformed type of change: {0}")]
TypeOfChangeParseError(LowLevelDeserializationError),
#[error("Malformed node address: {0}")]
NodeAddressParseError(LowLevelDeserializationError),
#[error("Unknown type of change: {0}")]
UnknownTypeOfChange(String),
}
#[non_exhaustive]
#[derive(Debug, Error, Clone)]
pub enum PreparedParseError {
#[error("Malformed prepared statement's id length: {0}")]
IdLengthParseError(LowLevelDeserializationError),
#[error("Invalid result metadata: {0}")]
ResultMetadataParseError(ResultMetadataParseError),
#[error("Invalid prepared metadata: {0}")]
PreparedMetadataParseError(PreparedMetadataParseError),
#[error("Non-zero paging state in result metadata: {0:?}")]
NonZeroPagingState(Arc<[u8]>),
}
#[non_exhaustive]
#[derive(Debug, Error, Clone)]
pub enum RawRowsAndPagingStateResponseParseError {
#[error("Malformed metadata flags: {0}")]
FlagsParseError(LowLevelDeserializationError),
#[error("Malformed column count: {0}")]
ColumnCountParseError(LowLevelDeserializationError),
#[error("Malformed paging state: {0}")]
PagingStateParseError(LowLevelDeserializationError),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum PreparedMetadataParseError {
#[error("Malformed metadata flags: {0}")]
FlagsParseError(LowLevelDeserializationError),
#[error("Malformed column count: {0}")]
ColumnCountParseError(LowLevelDeserializationError),
#[error("Malformed partition key count: {0}")]
PkCountParseError(LowLevelDeserializationError),
#[error("Malformed partition key index: {0}")]
PkIndexParseError(LowLevelDeserializationError),
#[error("Invalid global table spec: {0}")]
GlobalTableSpecParseError(#[from] TableSpecParseError),
#[error("Invalid column spec: {0}")]
ColumnSpecParseError(#[from] ColumnSpecParseError),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum ResultMetadataAndRowsCountParseError {
#[error("Failed to lazily deserialize result metadata: {0}")]
ResultMetadataParseError(#[from] ResultMetadataParseError),
#[error("Malformed rows count: {0}")]
RowsCountParseError(LowLevelDeserializationError),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum ResultMetadataParseError {
#[error("Malformed metadata flags: {0}")]
FlagsParseError(LowLevelDeserializationError),
#[error("Malformed column count: {0}")]
ColumnCountParseError(LowLevelDeserializationError),
#[error("Malformed paging state: {0}")]
PagingStateParseError(LowLevelDeserializationError),
#[error("Invalid global table spec: {0}")]
GlobalTableSpecParseError(#[from] TableSpecParseError),
#[error("Invalid column spec: {0}")]
ColumnSpecParseError(#[from] ColumnSpecParseError),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum TableSpecParseError {
#[error("Malformed keyspace name: {0}")]
MalformedKeyspaceName(LowLevelDeserializationError),
#[error("Malformed table name: {0}")]
MalformedTableName(LowLevelDeserializationError),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
#[error("Column spec deserialization failed, column index: {column_index}, error: {kind}")]
pub struct ColumnSpecParseError {
pub column_index: usize,
pub kind: ColumnSpecParseErrorKind,
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum ColumnSpecParseErrorKind {
#[error("Invalid table spec: {0}")]
TableSpecParseError(#[from] TableSpecParseError),
#[error("Table spec differs across columns - got specs: {0:?} and {1:?}")]
TableSpecDiffersAcrossColumns(TableSpec<'static>, TableSpec<'static>),
#[error("Malformed column name: {0}")]
ColumnNameParseError(#[from] LowLevelDeserializationError),
#[error("Invalid column type: {0}")]
ColumnTypeParseError(#[from] CqlTypeParseError),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum CqlTypeParseError {
#[error("Malformed type id: {0}")]
TypeIdParseError(LowLevelDeserializationError),
#[error("Malformed custom type name: {0}")]
CustomTypeNameParseError(LowLevelDeserializationError),
#[error("Malformed name of UDT keyspace: {0}")]
UdtKeyspaceNameParseError(LowLevelDeserializationError),
#[error("Malformed UDT name: {0}")]
UdtNameParseError(LowLevelDeserializationError),
#[error("Malformed UDT fields count: {0}")]
UdtFieldsCountParseError(LowLevelDeserializationError),
#[error("Malformed UDT's field name: {0}")]
UdtFieldNameParseError(LowLevelDeserializationError),
#[error("Malformed tuple length: {0}")]
TupleLengthParseError(LowLevelDeserializationError),
#[error("CQL Type not yet implemented, id: {0}")]
TypeNotImplemented(u16),
}
#[non_exhaustive]
#[derive(Error, Debug, Clone)]
pub enum LowLevelDeserializationError {
#[error(transparent)]
IoError(Arc<std::io::Error>),
#[error(transparent)]
TryFromIntError(#[from] std::num::TryFromIntError),
#[error(transparent)]
TryFromSliceError(#[from] std::array::TryFromSliceError),
#[error("Not enough bytes! expected: {expected}, received: {received}")]
TooFewBytesReceived { expected: usize, received: usize },
#[error("Invalid value length: {0}")]
InvalidValueLength(i32),
#[error("Unknown consistency: {0}")]
UnknownConsistency(#[from] TryFromPrimitiveError<u16>),
#[error("Invalid inet bytes length: {0}. Accepted lengths are 4 and 16 bytes.")]
InvalidInetLength(u8),
#[error("UTF8 deserialization failed: {0}")]
UTF8DeserializationError(#[from] std::str::Utf8Error),
}
impl From<std::io::Error> for LowLevelDeserializationError {
fn from(value: std::io::Error) -> Self {
Self::IoError(Arc::new(value))
}
}