diff --git a/scylla-cql/src/types/deserialize/mod.rs b/scylla-cql/src/types/deserialize/mod.rs index 639fc6255..066e3d9e1 100644 --- a/scylla-cql/src/types/deserialize/mod.rs +++ b/scylla-cql/src/types/deserialize/mod.rs @@ -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; @@ -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); impl TypeCheckError { @@ -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(&self) -> Option<&T> { + self.0.downcast_ref() + } } /// An error indicating that a failure happened during deserialization. @@ -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); impl DeserializationError { @@ -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(&self) -> Option<&T> { + self.0.downcast_ref() } } diff --git a/scylla-cql/src/types/serialize/mod.rs b/scylla-cql/src/types/serialize/mod.rs index b086dd5dc..3bc3424dc 100644 --- a/scylla-cql/src/types/serialize/mod.rs +++ b/scylla-cql/src/types/serialize/mod.rs @@ -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; @@ -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); impl SerializationError { @@ -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(&self) -> Option<&T> { + self.0.downcast_ref() } } diff --git a/scylla/src/transport/connection.rs b/scylla/src/transport/connection.rs index a7a476637..1e2eaddac 100644 --- a/scylla/src/transport/connection.rs +++ b/scylla/src/transport/connection.rs @@ -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."), }; diff --git a/scylla/src/transport/errors.rs b/scylla/src/transport/errors.rs index 8ef4a9943..aee6ab1f3 100644 --- a/scylla/src/transport/errors.rs +++ b/scylla/src/transport/errors.rs @@ -740,15 +740,15 @@ impl ConnectionSetupRequestError { pub struct BrokenConnectionError(Arc); impl BrokenConnectionError { - /// Retrieve an error reason. - pub fn get_reason(&self) -> &Arc { - &self.0 + /// Retrieve an error reason by downcasting to specific type. + pub fn downcast_ref(&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`. #[derive(Error, Debug)] #[non_exhaustive]