Skip to content

Commit 01255af

Browse files
authored
Merge pull request #1087 from muzarski/pubify-arced-errors
errors: expose pub getters for type-erased errors
2 parents 8a7bf30 + 2a23f06 commit 01255af

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

scylla-cql/src/types/deserialize/mod.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ pub use row::DeserializeRow;
179179
pub use value::DeserializeValue;
180180

181181
use std::error::Error;
182-
use std::fmt::Display;
183182
use std::sync::Arc;
184183

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

208207
impl TypeCheckError {
@@ -211,6 +210,11 @@ impl TypeCheckError {
211210
pub fn new(err: impl std::error::Error + Send + Sync + 'static) -> Self {
212211
Self(Arc::new(err))
213212
}
213+
214+
/// Retrieve an error reason by downcasting to specific type.
215+
pub fn downcast_ref<T: std::error::Error + 'static>(&self) -> Option<&T> {
216+
self.0.downcast_ref()
217+
}
214218
}
215219

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

233238
impl DeserializationError {
@@ -236,11 +241,10 @@ impl DeserializationError {
236241
pub fn new(err: impl Error + Send + Sync + 'static) -> Self {
237242
Self(Arc::new(err))
238243
}
239-
}
240244

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

scylla-cql/src/types/serialize/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

77
use thiserror::Error;
88

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

3839
impl SerializationError {
@@ -41,10 +42,9 @@ impl SerializationError {
4142
pub fn new(err: impl Error + Send + Sync + 'static) -> SerializationError {
4243
SerializationError(Arc::new(err))
4344
}
44-
}
4545

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

scylla/src/transport/connection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2780,7 +2780,7 @@ mod tests {
27802780
let err = error_receiver.await.unwrap();
27812781
let err_inner: &BrokenConnectionErrorKind = match err {
27822782
crate::transport::connection::ConnectionError::BrokenConnection(ref e) => {
2783-
e.get_reason().downcast_ref().unwrap()
2783+
e.downcast_ref().unwrap()
27842784
}
27852785
_ => panic!("Bad error type. Expected keepalive timeout."),
27862786
};

scylla/src/transport/errors.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,15 +740,15 @@ impl ConnectionSetupRequestError {
740740
pub struct BrokenConnectionError(Arc<dyn Error + Sync + Send>);
741741

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

749749
/// A reason why connection was broken.
750750
///
751-
/// See [`BrokenConnectionError::get_reason()`].
751+
/// See [`BrokenConnectionError::downcast_ref()`].
752752
/// You can retrieve the actual type by downcasting `Arc<dyn Error>`.
753753
#[derive(Error, Debug)]
754754
#[non_exhaustive]

0 commit comments

Comments
 (0)