Skip to content

Commit

Permalink
Add std::error::Error impls (#217)
Browse files Browse the repository at this point in the history
* Add std::error::Error impls

* Use source instead of cause.

* Fix warning.
  • Loading branch information
tomusdrw committed May 30, 2019
1 parent 5f4c915 commit 21cb38c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -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
Expand Down
33 changes: 17 additions & 16 deletions src/contract/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -19,25 +19,23 @@ pub enum Error {
Api(ApiError),
}

impl From<EthError> for Error {
fn from(e: EthError) -> Self {
Error::Abi(e)
}
}

impl From<ApiError> 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)]
Expand All @@ -47,9 +45,12 @@ pub mod deploy {
ContractDeploymentFailure(H256),
}

impl From<ApiError> 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,
}
}
}
}
21 changes: 10 additions & 11 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -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")]
Expand All @@ -30,15 +30,14 @@ pub enum Error {
Internal,
}

impl From<IoError> for Error {
fn from(e: IoError) -> Self {
Error::Io(e)
}
}

impl From<RPCError> 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),
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/trace_filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub struct Trace {
pub block_hash: H256,
#[serde(rename = "type")]
action_type: ActionType,
// Error
/// Error
pub error: Option<String>,
}

Expand Down

0 comments on commit 21cb38c

Please sign in to comment.