Unify error enums in substrate and ethereum clients with thiserror (#1094)

* Unify error enums in substrate and ethereum clients with `thiserror`

Related to https://github.com/paritytech/parity-bridges-common/issues/857

* Add license pre-amble

* rustfmt

* Fix spelling
This commit is contained in:
Vladislav
2021-10-22 13:15:50 +03:00
committed by Bastian Köcher
parent 7b4f1c2236
commit 5842968273
48 changed files with 482 additions and 381 deletions
@@ -17,3 +17,4 @@ log = "0.4.11"
relay-utils = { path = "../utils" }
tokio = "1.8"
web3 = { git = "https://github.com/svyatonik/rust-web3.git", branch = "bump-deps" }
thiserror = "1.0.26"
+13 -36
View File
@@ -20,49 +20,47 @@ use crate::types::U256;
use jsonrpsee_ws_client::types::Error as RpcError;
use relay_utils::MaybeConnectionError;
use thiserror::Error;
/// Result type used by Ethereum client.
pub type Result<T> = std::result::Result<T, Error>;
/// Errors that can occur only when interacting with
/// an Ethereum node through RPC.
#[derive(Debug)]
#[derive(Debug, Error)]
pub enum Error {
/// IO error.
Io(std::io::Error),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
/// An error that can occur when making an HTTP request to
/// an JSON-RPC client.
RpcError(RpcError),
#[error("RPC error: {0}")]
RpcError(#[from] RpcError),
/// Failed to parse response.
#[error("Response parse failed: {0}")]
ResponseParseFailed(String),
/// We have received a header with missing fields.
#[error("Incomplete Ethereum Header Received (missing some of required fields - hash, number, logs_bloom).")]
IncompleteHeader,
/// We have received a transaction missing a `raw` field.
#[error("Incomplete Ethereum Transaction (missing required field - raw).")]
IncompleteTransaction,
/// An invalid Substrate block number was received from
/// an Ethereum node.
#[error("Received an invalid Substrate block from Ethereum Node.")]
InvalidSubstrateBlockNumber,
/// An invalid index has been received from an Ethereum node.
#[error("Received an invalid incomplete index from Ethereum Node.")]
InvalidIncompleteIndex,
/// The client we're connected to is not synced, so we can't rely on its state. Contains
/// number of unsynced headers.
#[error("Ethereum client is not synced: syncing {0} headers.")]
ClientNotSynced(U256),
/// Custom logic error.
#[error("{0}")]
Custom(String),
}
impl From<RpcError> for Error {
fn from(error: RpcError) -> Self {
Error::RpcError(error)
}
}
impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
Error::Io(error)
}
}
impl From<tokio::task::JoinError> for Error {
fn from(error: tokio::task::JoinError) -> Self {
Error::Custom(format!("Failed to wait tokio task: {}", error))
@@ -82,24 +80,3 @@ impl MaybeConnectionError for Error {
)
}
}
impl ToString for Error {
fn to_string(&self) -> String {
match self {
Self::Io(e) => e.to_string(),
Self::RpcError(e) => e.to_string(),
Self::ResponseParseFailed(e) => e.to_string(),
Self::IncompleteHeader => {
"Incomplete Ethereum Header Received (missing some of required fields - hash, number, logs_bloom)"
.to_string()
}
Self::IncompleteTransaction => "Incomplete Ethereum Transaction (missing required field - raw)".to_string(),
Self::InvalidSubstrateBlockNumber => "Received an invalid Substrate block from Ethereum Node".to_string(),
Self::InvalidIncompleteIndex => "Received an invalid incomplete index from Ethereum Node".to_string(),
Self::ClientNotSynced(missing_headers) => {
format!("Ethereum client is not synced: syncing {} headers", missing_headers)
}
Self::Custom(ref e) => e.clone(),
}
}
}