Skip to content

Commit aa2a3e2

Browse files
committed
errors: clean up errors related to pk and token extraction
I marked `PartitionKeyError` as non_exhaustive. Narrowed the return type of remaining funtions that returned `QueryError` to `PartitionKeyError`. Added an explicit conversion function PartitionKeyError -> QueryError.
1 parent 918e522 commit aa2a3e2

File tree

4 files changed

+42
-39
lines changed

4 files changed

+42
-39
lines changed

scylla/src/client/pager.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,9 @@ impl QueryPager {
803803
) {
804804
Ok(res) => res.unzip(),
805805
Err(err) => {
806-
let (proof, _res) = ProvingSender::from(sender).send(Err(err)).await;
806+
let (proof, _res) = ProvingSender::from(sender)
807+
.send(Err(err.into_query_error()))
808+
.await;
807809
return proof;
808810
}
809811
};

scylla/src/client/session.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::policies::host_filter::HostFilter;
3131
use crate::policies::load_balancing::{self, RoutingInfo};
3232
use crate::policies::retry::{RequestInfo, RetryDecision, RetrySession};
3333
use crate::policies::speculative_execution;
34-
use crate::prepared_statement::PreparedStatement;
34+
use crate::prepared_statement::{PartitionKeyError, PreparedStatement};
3535
use crate::query::Query;
3636
#[allow(deprecated)]
3737
use crate::response::legacy_query_result::LegacyQueryResult;
@@ -1394,7 +1394,8 @@ where
13941394
let paging_state_ref = &paging_state;
13951395

13961396
let (partition_key, token) = prepared
1397-
.extract_partition_key_and_calculate_token(prepared.get_partitioner_name(), values_ref)?
1397+
.extract_partition_key_and_calculate_token(prepared.get_partitioner_name(), values_ref)
1398+
.map_err(PartitionKeyError::into_query_error)?
13981399
.unzip();
13991400

14001401
let execution_profile = prepared

scylla/src/statement/batch.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ pub(crate) mod batch_values {
217217
use scylla_cql::types::serialize::{RowWriter, SerializationError};
218218

219219
use crate::errors::QueryError;
220+
use crate::prepared_statement::PartitionKeyError;
220221
use crate::routing::Token;
221222

222223
use super::BatchStatement;
@@ -247,7 +248,9 @@ pub(crate) mod batch_values {
247248
.map(|o| o.is_some())
248249
})?;
249250
if did_write {
250-
let token = ps.calculate_token_untyped(&first_values)?;
251+
let token = ps
252+
.calculate_token_untyped(&first_values)
253+
.map_err(PartitionKeyError::into_query_error)?;
251254
(token, Some(first_values))
252255
} else {
253256
(None, None)

scylla/src/statement/prepared_statement.rs

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -232,25 +232,13 @@ impl PreparedStatement {
232232
&'ps self,
233233
partitioner_name: &'ps PartitionerName,
234234
serialized_values: &'ps SerializedValues,
235-
) -> Result<Option<(PartitionKey<'ps>, Token)>, QueryError> {
235+
) -> Result<Option<(PartitionKey<'ps>, Token)>, PartitionKeyError> {
236236
if !self.is_token_aware() {
237237
return Ok(None);
238238
}
239239

240-
let partition_key =
241-
self.extract_partition_key(serialized_values)
242-
.map_err(|err| match err {
243-
PartitionKeyExtractionError::NoPkIndexValue(_, _) => {
244-
ProtocolError::PartitionKeyExtraction
245-
}
246-
})?;
247-
let token = partition_key
248-
.calculate_token(partitioner_name)
249-
.map_err(|err| match err {
250-
TokenCalculationError::ValueTooLong(values_len) => {
251-
QueryError::BadQuery(BadQuery::ValuesTooLongForKey(values_len, u16::MAX.into()))
252-
}
253-
})?;
240+
let partition_key = self.extract_partition_key(serialized_values)?;
241+
let token = partition_key.calculate_token(partitioner_name)?;
254242

255243
Ok(Some((partition_key, token)))
256244
}
@@ -262,7 +250,10 @@ impl PreparedStatement {
262250
// As this function creates a `PartitionKey`, it is intended rather for external usage (by users).
263251
// For internal purposes, `PartitionKey::calculate_token()` is preferred, as `PartitionKey`
264252
// is either way used internally, among others for display in traces.
265-
pub fn calculate_token(&self, values: &impl SerializeRow) -> Result<Option<Token>, QueryError> {
253+
pub fn calculate_token(
254+
&self,
255+
values: &impl SerializeRow,
256+
) -> Result<Option<Token>, PartitionKeyError> {
266257
self.calculate_token_untyped(&self.serialize_values(values)?)
267258
}
268259

@@ -271,7 +262,7 @@ impl PreparedStatement {
271262
pub(crate) fn calculate_token_untyped(
272263
&self,
273264
values: &SerializedValues,
274-
) -> Result<Option<Token>, QueryError> {
265+
) -> Result<Option<Token>, PartitionKeyError> {
275266
self.extract_partition_key_and_calculate_token(&self.partitioner_name, values)
276267
.map(|opt| opt.map(|(_pk, token)| token))
277268
}
@@ -485,31 +476,37 @@ pub enum TokenCalculationError {
485476
ValueTooLong(usize),
486477
}
487478

479+
/// An error returned by [`PreparedStatement::compute_partition_key()`].
488480
#[derive(Clone, Debug, Error)]
481+
#[non_exhaustive]
489482
pub enum PartitionKeyError {
483+
/// Failed to extract partition key.
490484
#[error(transparent)]
491-
PartitionKeyExtraction(PartitionKeyExtractionError),
492-
#[error(transparent)]
493-
TokenCalculation(TokenCalculationError),
494-
#[error(transparent)]
495-
Serialization(SerializationError),
496-
}
485+
PartitionKeyExtraction(#[from] PartitionKeyExtractionError),
497486

498-
impl From<PartitionKeyExtractionError> for PartitionKeyError {
499-
fn from(err: PartitionKeyExtractionError) -> Self {
500-
Self::PartitionKeyExtraction(err)
501-
}
502-
}
487+
/// Failed to calculate token.
488+
#[error(transparent)]
489+
TokenCalculation(#[from] TokenCalculationError),
503490

504-
impl From<TokenCalculationError> for PartitionKeyError {
505-
fn from(err: TokenCalculationError) -> Self {
506-
Self::TokenCalculation(err)
507-
}
491+
/// Failed to serialize values required to compute partition key.
492+
#[error(transparent)]
493+
Serialization(#[from] SerializationError),
508494
}
509495

510-
impl From<SerializationError> for PartitionKeyError {
511-
fn from(err: SerializationError) -> Self {
512-
Self::Serialization(err)
496+
impl PartitionKeyError {
497+
/// Converts the error to [`QueryError`].
498+
pub fn into_query_error(self) -> QueryError {
499+
match self {
500+
PartitionKeyError::PartitionKeyExtraction(_) => {
501+
QueryError::ProtocolError(ProtocolError::PartitionKeyExtraction)
502+
}
503+
PartitionKeyError::TokenCalculation(TokenCalculationError::ValueTooLong(
504+
values_len,
505+
)) => QueryError::BadQuery(BadQuery::ValuesTooLongForKey(values_len, u16::MAX.into())),
506+
PartitionKeyError::Serialization(err) => {
507+
QueryError::BadQuery(BadQuery::SerializationError(err))
508+
}
509+
}
513510
}
514511
}
515512

0 commit comments

Comments
 (0)