sqlx_scylladb_core/
error.rs

1use std::error::Error as StdError;
2
3use scylla::{
4    cluster::metadata::ColumnType,
5    errors::{
6        DeserializationError, ExecutionError, IntoRowsResultError, NewSessionError,
7        PagerExecutionError, PrepareError, RowsError, SerializationError, TypeCheckError,
8        UseKeyspaceError,
9    },
10};
11use sqlx::error::{DatabaseError, ErrorKind};
12use sqlx_core::ext::ustr::UStr;
13use thiserror::Error;
14
15/// Represents all the ways a method can fail within ScyllaDB.
16#[derive(Debug, Error)]
17#[error(transparent)]
18pub enum ScyllaDBError {
19    /// There is an error in the options.
20    #[error("Configuration error. {0}")]
21    ConfigurationError(String),
22    /// Error occurred while creating the session.
23    NewSessionError(#[from] NewSessionError),
24    /// There is an error in the specified keyspace.
25    UseKeyspaceError(#[from] UseKeyspaceError),
26    /// Error occurred while preparing the statement.
27    PrepareError(#[from] PrepareError),
28    /// Error occurred while converting to rows result.
29    IntoRowsResultError(#[from] IntoRowsResultError),
30    /// Error occurred while retrieving the row.
31    RowsError(#[from] RowsError),
32    /// Error occurred while type checking.
33    TypeCheckError(#[from] TypeCheckError),
34    /// Error occurred while serialization.
35    SerializationError(#[from] SerializationError),
36    /// Error occurred while deserialization.
37    DeserializationError(#[from] DeserializationError),
38    /// Error occurred while execution.
39    ExecutionError(#[from] ExecutionError),
40    /// Error occurred while pagination.
41    PagerExecutionError(#[from] PagerExecutionError),
42    /// Transaction is not started.
43    #[error("Transaction is not started.")]
44    TransactionNotStarted,
45    /// Attempted to retrieve data exceeding the number of columns.
46    #[error("Column index out of bounds. the len is {len}, but the index is {index}")]
47    ColumnIndexOutOfBounds {
48        /// index.
49        index: usize,
50        /// total size.
51        len: usize,
52    },
53    /// Column types do not match.
54    #[error("Column type is mismatched. expect: {expect:?}, actual: {actual:?}")]
55    ColumnTypeError {
56        /// expected column type.
57        expect: ColumnType<'static>,
58        /// actual column type.
59        actual: ColumnType<'static>,
60    },
61    /// Failed to acquire migration lock.
62    #[error("Failed to acquire migration lock.")]
63    MigrationLockError,
64    /// Column types do not match.
65    #[error("Mismatched column type {0}: {1:?}..")]
66    MismatchedColumnTypeError(UStr, ColumnType<'static>),
67    /// This column type is not supported.
68    #[error("Column type '{0:?}' is not supported.")]
69    ColumnTypeNotSupportedError(ColumnType<'static>),
70    /// The value is null.
71    #[error("{0:?} is null.")]
72    NullValueError(UStr),
73    /// Failed to acquire exclusive lock.
74    #[error("Exclusive lock error.")]
75    ExclusiveLockError,
76}
77
78impl DatabaseError for ScyllaDBError {
79    fn message(&self) -> &str {
80        match self {
81            ScyllaDBError::ConfigurationError(message) => &message,
82            ScyllaDBError::NewSessionError(_) => "New session error.",
83            ScyllaDBError::UseKeyspaceError(_) => "Use keyspace error.",
84            ScyllaDBError::PrepareError(_) => "Prepare error.",
85            ScyllaDBError::IntoRowsResultError(_) => "Into rows result error.",
86            ScyllaDBError::RowsError(_) => "Rows error.",
87            ScyllaDBError::TypeCheckError(_) => "Type check error.",
88            ScyllaDBError::SerializationError(_) => "Serialization error.",
89            ScyllaDBError::DeserializationError(_) => "Deserialization error.",
90            ScyllaDBError::ExecutionError(_) => "Execution error.",
91            ScyllaDBError::PagerExecutionError(_) => "Pager execution error.",
92            ScyllaDBError::TransactionNotStarted => "Transaction is not started.",
93            ScyllaDBError::ColumnIndexOutOfBounds { index: _, len: _ } => {
94                "Column index out of bounds."
95            }
96            ScyllaDBError::ColumnTypeError {
97                expect: _,
98                actual: _,
99            } => "Column type error.",
100            ScyllaDBError::MigrationLockError => "Migration lock error.",
101            ScyllaDBError::MismatchedColumnTypeError(_, _) => "Mismatched column type.",
102            ScyllaDBError::ColumnTypeNotSupportedError(_) => "Column type not supported.",
103            ScyllaDBError::NullValueError(_) => "Null value error",
104            ScyllaDBError::ExclusiveLockError => "Exclusive lock error.",
105        }
106    }
107
108    fn as_error(&self) -> &(dyn StdError + Send + Sync + 'static) {
109        self
110    }
111
112    fn as_error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {
113        self
114    }
115
116    fn into_error(self: Box<Self>) -> Box<dyn StdError + Send + Sync + 'static> {
117        self
118    }
119
120    fn kind(&self) -> ErrorKind {
121        ErrorKind::Other
122    }
123}