From 677fa1e2b5fdf065875f8601f7f9cb5e8541c2e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Wed, 3 Apr 2024 10:06:23 +0200 Subject: [PATCH] Do not lose error --- drink/src/errors.rs | 2 +- drink/src/session.rs | 10 +++++----- drink/src/session/error.rs | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/drink/src/errors.rs b/drink/src/errors.rs index a05bc39..960e74e 100644 --- a/drink/src/errors.rs +++ b/drink/src/errors.rs @@ -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}")] diff --git a/drink/src/session.rs b/drink/src/session.rs index 2f2f06f..5a6b0c1 100644 --- a/drink/src/session.rs +++ b/drink/src/session.rs @@ -470,13 +470,13 @@ where message: &str, args: &[S], endowment: Option>, - ) -> E { - self.call_internal::<_, Result<(), E>>(None, message, args, endowment) + ) -> Result { + Ok(self + .call_internal::<_, Result<(), E>>(None, message, args, endowment) .expect_err("Call should fail") - .decode_revert::>() - .expect("Call should be reverted") + .decode_revert::>()? .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 diff --git a/drink/src/session/error.rs b/drink/src/session/error.rs index d76be55..bf1cae7 100644 --- a/drink/src/session/error.rs +++ b/drink/src/session/error.rs @@ -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}")] @@ -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(&self) -> Option> { + pub fn decode_revert(&self) -> Result, 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()), } } }