Skip to content

Commit

Permalink
Do not lose error
Browse files Browse the repository at this point in the history
  • Loading branch information
pmikolajczyk41 committed Apr 3, 2024
1 parent 9f0c32a commit 677fa1e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion drink/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use thiserror::Error;

/// Main error type for the drink crate.
#[derive(Error, Debug)]
#[derive(Clone, Error, Debug)]
pub enum Error {
/// Externalities could not be initialized.
#[error("Failed to build storage: {0}")]
Expand Down
10 changes: 5 additions & 5 deletions drink/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,13 @@ where
message: &str,
args: &[S],
endowment: Option<BalanceOf<T::Runtime>>,
) -> E {
self.call_internal::<_, Result<(), E>>(None, message, args, endowment)
) -> Result<E, SessionError> {
Ok(self
.call_internal::<_, Result<(), E>>(None, message, args, endowment)
.expect_err("Call should fail")
.decode_revert::<Result<(), E>>()
.expect("Call should be reverted")
.decode_revert::<Result<(), E>>()?
.expect("Call should return an error")
.expect_err("Call should return an error")
.expect_err("Call should return an error"))
}

/// Calls a contract with a given address. In case of a successful call, returns the encoded
Expand Down
8 changes: 4 additions & 4 deletions drink/src/session/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use thiserror::Error;
use crate::errors::MessageResult;

/// Session specific errors.
#[derive(Error, Debug)]
#[derive(Clone, Error, Debug)]
pub enum SessionError {
/// Encoding data failed.
#[error("Encoding call data failed: {0}")]
Expand Down Expand Up @@ -43,12 +43,12 @@ pub enum SessionError {

impl SessionError {
/// Check if the error is a revert error and if so, decode the error message.
pub fn decode_revert<T: Decode>(&self) -> Option<MessageResult<T>> {
pub fn decode_revert<T: Decode>(&self) -> Result<MessageResult<T>, Self> {
match self {
SessionError::CallReverted(error) => {
Some(MessageResult::decode(&mut &error[..]).expect("Failed to decode error"))
Ok(MessageResult::decode(&mut &error[..]).expect("Failed to decode error"))
}
_ => None,
_ => Err(self.clone()),
}
}
}

0 comments on commit 677fa1e

Please sign in to comment.