From 21cb38c7203a026f292d65059272b88e2050626e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Drwi=C4=99ga?= Date: Thu, 30 May 2019 13:15:03 +0200 Subject: [PATCH] Add std::error::Error impls (#217) * Add std::error::Error impls * Use source instead of cause. * Fix warning. --- .editorconfig | 2 +- src/contract/error.rs | 33 +++++++++++++++++---------------- src/error.rs | 21 ++++++++++----------- src/types/trace_filtering.rs | 2 +- 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/.editorconfig b/.editorconfig index 37ed9966..9810695a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,7 +1,7 @@ root = true [*] indent_style=space -indent_size=2 +indent_size=4 tab_width=4 end_of_line=lf charset=utf-8 diff --git a/src/contract/error.rs b/src/contract/error.rs index aba390f4..c8d7e916 100644 --- a/src/contract/error.rs +++ b/src/contract/error.rs @@ -3,10 +3,10 @@ use ethabi::Error as EthError; use crate::error::Error as ApiError; -use derive_more::Display; +use derive_more::{Display, From}; /// Contract error. -#[derive(Debug, Display)] +#[derive(Debug, Display, From)] pub enum Error { /// invalid output type requested by the caller #[display(fmt = "Invalid output type: {}", _0)] @@ -19,25 +19,23 @@ pub enum Error { Api(ApiError), } -impl From for Error { - fn from(e: EthError) -> Self { - Error::Abi(e) - } -} - -impl From for Error { - fn from(e: ApiError) -> Self { - Error::Api(e) +impl std::error::Error for Error { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match *self { + Error::InvalidOutputType(_) => None, + Error::Abi(ref e) => Some(e), + Error::Api(ref e) => Some(e), + } } } pub mod deploy { use crate::error::Error as ApiError; use crate::types::H256; - use derive_more::Display; + use derive_more::{Display, From}; /// Contract deployment error. - #[derive(Debug, Display)] + #[derive(Debug, Display, From)] pub enum Error { /// Rpc error #[display(fmt = "Api error: {}", _0)] @@ -47,9 +45,12 @@ pub mod deploy { ContractDeploymentFailure(H256), } - impl From for Error { - fn from(e: ApiError) -> Self { - Error::Api(e) + impl std::error::Error for Error { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match *self { + Error::Api(ref e) => Some(e), + Error::ContractDeploymentFailure(_) => None, + } } } } diff --git a/src/error.rs b/src/error.rs index 474349cb..86400f0d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,11 +1,11 @@ //! Web3 Error use crate::rpc::error::Error as RPCError; -use derive_more::Display; +use derive_more::{Display, From}; use serde_json::Error as SerdeError; use std::io::Error as IoError; /// Errors which can occur when attempting to generate resource uri. -#[derive(Debug, Display)] +#[derive(Debug, Display, From)] pub enum Error { /// server is unreachable #[display(fmt = "Server is unreachable")] @@ -30,15 +30,14 @@ pub enum Error { Internal, } -impl From for Error { - fn from(e: IoError) -> Self { - Error::Io(e) - } -} - -impl From for Error { - fn from(e: RPCError) -> Self { - Error::Rpc(e) +impl std::error::Error for Error { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + use self::Error::*; + match *self { + Unreachable | Decoder(_) | InvalidResponse(_) | Transport(_) | Internal => None, + Rpc(ref e) => Some(e), + Io(ref e) => Some(e), + } } } diff --git a/src/types/trace_filtering.rs b/src/types/trace_filtering.rs index c619e505..7a358a50 100644 --- a/src/types/trace_filtering.rs +++ b/src/types/trace_filtering.rs @@ -102,7 +102,7 @@ pub struct Trace { pub block_hash: H256, #[serde(rename = "type")] action_type: ActionType, - // Error + /// Error pub error: Option, }