Skip to content

Commit

Permalink
errors: expose pub getters for type-erased errors
Browse files Browse the repository at this point in the history
Sometimes, user might want to downcast `Arc<dyn Error>` to specific
error type.

Actually, there is a use case for this in cpp-rust-driver,
where we need to define custom `SerializeRow` implementation. It returns
a custom error type. We would like to match against it during
rust-to-C error conversion - downcasting is thus required.
  • Loading branch information
muzarski committed Oct 10, 2024
1 parent 4aa7305 commit bf668be
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
10 changes: 10 additions & 0 deletions scylla-cql/src/types/deserialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ impl TypeCheckError {
pub fn new(err: impl std::error::Error + Send + Sync + 'static) -> Self {
Self(Arc::new(err))
}

/// Retrieve an error reason.
pub fn get_reason(&self) -> &Arc<dyn std::error::Error + Send + Sync> {
&self.0
}
}

/// An error indicating that a failure happened during deserialization.
Expand All @@ -236,6 +241,11 @@ impl DeserializationError {
pub fn new(err: impl Error + Send + Sync + 'static) -> Self {
Self(Arc::new(err))
}

/// Retrieve an error reason.
pub fn get_reason(&self) -> &Arc<dyn Error + Send + Sync> {
&self.0
}
}

impl Display for DeserializationError {
Expand Down
5 changes: 5 additions & 0 deletions scylla-cql/src/types/serialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ impl SerializationError {
pub fn new(err: impl Error + Send + Sync + 'static) -> SerializationError {
SerializationError(Arc::new(err))
}

/// Retrieve an error reason.
pub fn get_reason(&self) -> &Arc<dyn Error + Send + Sync> {
&self.0
}
}

impl Display for SerializationError {
Expand Down

0 comments on commit bf668be

Please sign in to comment.