Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors: introduce PrepareError #1193

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions scylla/src/client/caching_session.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::batch::{Batch, BatchStatement};
#[allow(deprecated)]
use crate::client::pager::LegacyRowIterator;
use crate::errors::ExecutionError;
use crate::errors::{ExecutionError, PrepareError};
use crate::prepared_statement::PreparedStatement;
use crate::query::Query;
#[allow(deprecated)]
Expand Down Expand Up @@ -276,15 +276,15 @@ where
pub async fn add_prepared_statement(
&self,
query: impl Into<&Query>,
) -> Result<PreparedStatement, ExecutionError> {
) -> Result<PreparedStatement, PrepareError> {
self.add_prepared_statement_owned(query.into().clone())
.await
}

async fn add_prepared_statement_owned(
&self,
query: impl Into<Query>,
) -> Result<PreparedStatement, ExecutionError> {
) -> Result<PreparedStatement, PrepareError> {
let query = query.into();

if let Some(raw) = self.cache.get(&query.contents) {
Expand Down
10 changes: 5 additions & 5 deletions scylla/src/client/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::cluster::node::CloudEndpoint;
use crate::cluster::node::{InternalKnownNode, KnownNode, NodeRef};
use crate::cluster::{Cluster, ClusterNeatDebug, ClusterState};
use crate::errors::{
BadQuery, ExecutionError, MetadataError, NewSessionError, ProtocolError, RequestAttemptError,
RequestError, TracingProtocolError, UseKeyspaceError,
BadQuery, ExecutionError, MetadataError, NewSessionError, PrepareError, ProtocolError,
RequestAttemptError, RequestError, TracingProtocolError, UseKeyspaceError,
};
use crate::frame::response::result;
#[cfg(feature = "ssl")]
Expand Down Expand Up @@ -1300,7 +1300,7 @@ where
pub async fn prepare(
&self,
query: impl Into<Query>,
) -> Result<PreparedStatement, ExecutionError> {
) -> Result<PreparedStatement, PrepareError> {
let query = query.into();
let query_ref = &query;

Expand All @@ -1319,12 +1319,12 @@ where
let first_ok: Result<PreparedStatement, RequestAttemptError> =
results.by_ref().find_or_first(Result::is_ok).unwrap();
let mut prepared: PreparedStatement =
first_ok.map_err(RequestAttemptError::into_execution_error)?;
first_ok.map_err(|first_attempt| PrepareError::AllAttemptsFailed { first_attempt })?;

// Validate prepared ids equality
for statement in results.flatten() {
if prepared.get_id() != statement.get_id() {
return Err(ProtocolError::PreparedStatementIdsMismatch.into());
return Err(PrepareError::PreparedStatementIdsMismatch);
}

// Collect all tracing ids from prepare() queries in the final result
Expand Down
4 changes: 2 additions & 2 deletions scylla/src/cluster/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::errors::{BadQuery, ExecutionError};
use crate::errors::{BadQuery, ConnectionPoolError};
use crate::network::{Connection, PoolConfig, VerifiedKeyspaceName};
use crate::policies::host_filter::HostFilter;
use crate::prepared_statement::TokenCalculationError;
Expand Down Expand Up @@ -268,7 +268,7 @@ impl ClusterState {
/// Returns nonempty iterator of working connections to all shards.
pub(crate) fn iter_working_connections(
&self,
) -> Result<impl Iterator<Item = Arc<Connection>> + '_, ExecutionError> {
) -> Result<impl Iterator<Item = Arc<Connection>> + '_, ConnectionPoolError> {
// The returned iterator is nonempty by nonemptiness invariant of `self.known_peers`.
assert!(!self.known_peers.is_empty());
let mut peers_iter = self.known_peers.values();
Expand Down
30 changes: 24 additions & 6 deletions scylla/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ use crate::response::query_result::{IntoRowsResultError, SingleRowError};
#[non_exhaustive]
#[allow(deprecated)]
pub enum ExecutionError {
/// Failed to prepare the statement.
/// Applies to unprepared statements with non-empty value parameters.
#[error("Failed to prepare the statement: {0}")]
PrepareError(#[from] PrepareError),

/// Database sent a response containing some error with a message
#[error("Database returned an error: {0}, Error message: {1}")]
DbError(DbError, String),
Expand Down Expand Up @@ -150,6 +155,25 @@ impl From<response::Error> for ExecutionError {
}
}

/// An error returned by [`Session::prepare()`][crate::client::session::Session::prepare].
#[derive(Error, Debug, Clone)]
#[non_exhaustive]
pub enum PrepareError {
/// Failed to find a node with working connection pool.
#[error("Failed to find a node with working connection pool: {0}")]
ConnectionPoolError(#[from] ConnectionPoolError),

/// Failed to prepare statement on every connection from the pool.
#[error("Preparation failed on every connection from the selected pool. First attempt error: {first_attempt}")]
AllAttemptsFailed { first_attempt: RequestAttemptError },

/// Prepared statement id mismatch.
#[error(
"Prepared statement id mismatch between multiple connections - all result ids should be equal."
)]
PreparedStatementIdsMismatch,
}

/// Error that occurred during session creation
#[derive(Error, Debug, Clone)]
#[non_exhaustive]
Expand Down Expand Up @@ -188,12 +212,6 @@ pub enum ProtocolError {
)]
UnexpectedResponse(CqlResponseKind),

/// Prepared statement id mismatch.
#[error(
"Prepared statement id mismatch between multiple connections - all result ids should be equal."
)]
PreparedStatementIdsMismatch,

/// Prepared statement id changed after repreparation.
#[error(
"Prepared statement id changed after repreparation; md5 sum (computed from the query string) should stay the same;\
Expand Down