Skip to content

Commit

Permalink
Merge pull request #1087 from muzarski/pubify-arced-errors
Browse files Browse the repository at this point in the history
errors: expose pub getters for type-erased errors
  • Loading branch information
Lorak-mmk authored Oct 16, 2024
2 parents 8a7bf30 + 2a23f06 commit 01255af
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
16 changes: 10 additions & 6 deletions scylla-cql/src/types/deserialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ pub use row::DeserializeRow;
pub use value::DeserializeValue;

use std::error::Error;
use std::fmt::Display;
use std::sync::Arc;

use thiserror::Error;
Expand All @@ -202,7 +201,7 @@ use thiserror::Error;
/// It won't be returned by the `Session` directly, but it might be nested
/// in the [`row::BuiltinTypeCheckError`].
#[derive(Debug, Clone, Error)]
#[error(transparent)]
#[error("TypeCheckError: {0}")]
pub struct TypeCheckError(pub(crate) Arc<dyn std::error::Error + Send + Sync>);

impl TypeCheckError {
Expand All @@ -211,6 +210,11 @@ impl TypeCheckError {
pub fn new(err: impl std::error::Error + Send + Sync + 'static) -> Self {
Self(Arc::new(err))
}

/// Retrieve an error reason by downcasting to specific type.
pub fn downcast_ref<T: std::error::Error + 'static>(&self) -> Option<&T> {
self.0.downcast_ref()
}
}

/// An error indicating that a failure happened during deserialization.
Expand All @@ -228,6 +232,7 @@ impl TypeCheckError {
/// It won't be returned by the `Session` directly, but it might be nested
/// in the [`row::BuiltinDeserializationError`].
#[derive(Debug, Clone, Error)]
#[error("DeserializationError: {0}")]
pub struct DeserializationError(Arc<dyn Error + Send + Sync>);

impl DeserializationError {
Expand All @@ -236,11 +241,10 @@ impl DeserializationError {
pub fn new(err: impl Error + Send + Sync + 'static) -> Self {
Self(Arc::new(err))
}
}

impl Display for DeserializationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "DeserializationError: {}", self.0)
/// Retrieve an error reason by downcasting to specific type.
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
self.0.downcast_ref()
}
}

Expand Down
10 changes: 5 additions & 5 deletions scylla-cql/src/types/serialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//! Types and traits related to serialization of values to the CQL format.
use std::{error::Error, fmt::Display, sync::Arc};
use std::{error::Error, sync::Arc};

use thiserror::Error;

Expand Down Expand Up @@ -33,6 +33,7 @@ pub use writers::{CellValueBuilder, CellWriter, RowWriter};
/// as an argument to the statement, and rewriting it using the new
/// `SerializeRow` interface fails.
#[derive(Debug, Clone, Error)]
#[error("SerializationError: {0}")]
pub struct SerializationError(Arc<dyn Error + Send + Sync>);

impl SerializationError {
Expand All @@ -41,10 +42,9 @@ impl SerializationError {
pub fn new(err: impl Error + Send + Sync + 'static) -> SerializationError {
SerializationError(Arc::new(err))
}
}

impl Display for SerializationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "SerializationError: {}", self.0)
/// Retrieve an error reason by downcasting to specific type.
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
self.0.downcast_ref()
}
}
2 changes: 1 addition & 1 deletion scylla/src/transport/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2780,7 +2780,7 @@ mod tests {
let err = error_receiver.await.unwrap();
let err_inner: &BrokenConnectionErrorKind = match err {
crate::transport::connection::ConnectionError::BrokenConnection(ref e) => {
e.get_reason().downcast_ref().unwrap()
e.downcast_ref().unwrap()
}
_ => panic!("Bad error type. Expected keepalive timeout."),
};
Expand Down
8 changes: 4 additions & 4 deletions scylla/src/transport/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,15 +740,15 @@ impl ConnectionSetupRequestError {
pub struct BrokenConnectionError(Arc<dyn Error + Sync + Send>);

impl BrokenConnectionError {
/// Retrieve an error reason.
pub fn get_reason(&self) -> &Arc<dyn Error + Sync + Send> {
&self.0
/// Retrieve an error reason by downcasting to specific type.
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
self.0.downcast_ref()
}
}

/// A reason why connection was broken.
///
/// See [`BrokenConnectionError::get_reason()`].
/// See [`BrokenConnectionError::downcast_ref()`].
/// You can retrieve the actual type by downcasting `Arc<dyn Error>`.
#[derive(Error, Debug)]
#[non_exhaustive]
Expand Down

0 comments on commit 01255af

Please sign in to comment.