refactor: small error handling improvements

buddies-main-deployment
James Prestwich 4 years ago
parent e95634a610
commit 4555178942
No known key found for this signature in database
GPG Key ID: 75A7F5C06D747046
  1. 29
      rust/optics-core/src/lib.rs
  2. 6
      rust/optics-core/src/traits/mod.rs

@ -1,5 +1,7 @@
//! Optics. OPTimistic Interchain Communication
//!
//! This crate contains core primitives, traits, and types for Optics
//! implementations.
#![forbid(unsafe_code)]
#![warn(missing_docs)]
@ -25,7 +27,7 @@ use ethers_signers::Signer;
use sha3::{Digest, Keccak256};
use std::convert::TryFrom;
use crate::utils::*;
use crate::{traits::ChainCommunicationError, utils::*};
/// Error types for Optics
#[derive(Debug, thiserror::Error)]
@ -45,6 +47,12 @@ pub enum OpticsError {
/// improper update and is slashable
#[error("Update has unknown new root: {0}")]
UnknownNewRoot(H256),
/// IO error from Read/Write usage
#[error(transparent)]
IoError(#[from] std::io::Error),
/// ChainCommunicationError
#[error(transparent)]
ChainCommunicationError(#[from] ChainCommunicationError),
}
/// Simple trait for types with a canonical encoding
@ -64,11 +72,8 @@ pub trait Encode {
/// Simple trait for types with a canonical encoding
pub trait Decode {
/// Error type
type Error;
/// Try to read from some source
fn read_from<R>(reader: &mut R) -> Result<Self, Self::Error>
fn read_from<R>(reader: &mut R) -> Result<Self, OpticsError>
where
R: std::io::Read,
Self: Sized;
@ -85,19 +90,16 @@ impl Encode for Signature {
}
impl Decode for Signature {
type Error = SignatureError;
fn read_from<R>(reader: &mut R) -> Result<Self, Self::Error>
fn read_from<R>(reader: &mut R) -> Result<Self, OpticsError>
where
R: std::io::Read,
{
let mut buf = [0u8; 65];
let len = reader
.read(&mut buf)
.map_err(|_| SignatureError::InvalidLength(0))?;
let len = reader.read(&mut buf)?;
if len != 65 {
Err(SignatureError::InvalidLength(len))
Err(SignatureError::InvalidLength(len).into())
} else {
Self::try_from(buf.as_ref())
Ok(Self::try_from(buf.as_ref())?)
}
}
}
@ -134,8 +136,7 @@ impl Encode for Message {
}
impl Decode for Message {
type Error = std::io::Error;
fn read_from<R>(reader: &mut R) -> Result<Self, Self::Error>
fn read_from<R>(reader: &mut R) -> Result<Self, OpticsError>
where
R: std::io::Read,
{

@ -44,13 +44,13 @@ impl From<TransactionReceipt> for TxOutcome {
/// Error type for chain communication
pub enum ChainCommunicationError {
/// Provider Error
#[error("{0}")]
#[error(transparent)]
ProviderError(#[from] ethers_providers::ProviderError),
/// Contract Error
#[error("{0}")]
#[error(transparent)]
ContractError(Box<dyn std::error::Error>),
/// Custom error or contract error
#[error("{0}")]
#[error(transparent)]
CustomError(#[from] Box<dyn std::error::Error>),
}

Loading…
Cancel
Save