Merge pull request #1116 from rphmeier/die_display

Have `die_with_error` use `fmt::Display` rather than Debug
This commit is contained in:
Marek Kotewicz
2016-05-24 20:00:50 +02:00
7 changed files with 234 additions and 3 deletions

View File

@@ -21,6 +21,7 @@ use bytes::*;
use secp256k1::{key, Secp256k1};
use rand::os::OsRng;
use sha3::Hashable;
use std::fmt;
/// Secret key for secp256k1 EC operations. 256 bit generic "hash" data.
pub type Secret = H256;
@@ -69,6 +70,20 @@ pub enum CryptoError {
Io(::std::io::Error),
}
impl fmt::Display for CryptoError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let msg = match *self {
CryptoError::InvalidSecret => "Invalid secret key".into(),
CryptoError::InvalidPublic => "Invalid public key".into(),
CryptoError::InvalidSignature => "Invalid EC signature".into(),
CryptoError::InvalidMessage => "Invalid AES message".into(),
CryptoError::Io(ref err) => format!("I/O error: {}", err),
};
f.write_fmt(format_args!("Crypto error ({})", msg))
}
}
impl From<::secp256k1::Error> for CryptoError {
fn from(e: ::secp256k1::Error) -> CryptoError {
match e {

View File

@@ -30,6 +30,16 @@ pub enum BaseDataError {
NegativelyReferencedHash(H256),
}
impl fmt::Display for BaseDataError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
BaseDataError::NegativelyReferencedHash(hash) =>
f.write_fmt(format_args!("Entry {} removed from database more times \
than it was added.", hash)),
}
}
}
#[derive(Debug)]
/// General error type which should be capable of representing all errors in ethcore.
pub enum UtilError {
@@ -57,6 +67,24 @@ pub enum UtilError {
BadSize,
}
impl fmt::Display for UtilError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
UtilError::Crypto(ref err) => f.write_fmt(format_args!("{}", err)),
UtilError::StdIo(ref err) => f.write_fmt(format_args!("{}", err)),
UtilError::Io(ref err) => f.write_fmt(format_args!("{}", err)),
UtilError::AddressParse(ref err) => f.write_fmt(format_args!("{}", err)),
UtilError::AddressResolve(Some(ref err)) => f.write_fmt(format_args!("{}", err)),
UtilError::AddressResolve(_) => f.write_str("Failed to resolve network address."),
UtilError::FromHex(ref err) => f.write_fmt(format_args!("{}", err)),
UtilError::BaseData(ref err) => f.write_fmt(format_args!("{}", err)),
UtilError::Network(ref err) => f.write_fmt(format_args!("{}", err)),
UtilError::Decoder(ref err) => f.write_fmt(format_args!("{}", err)),
UtilError::SimpleString(ref msg) => f.write_str(&msg),
UtilError::BadSize => f.write_str("Bad input size."),
}
}
}
#[derive(Debug, PartialEq, Eq)]
/// Error indicating an expected value was not found.
@@ -67,6 +95,12 @@ pub struct Mismatch<T: fmt::Debug> {
pub found: T,
}
impl<T: fmt::Debug + fmt::Display> fmt::Display for Mismatch<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_fmt(format_args!("Expected {}, found {}", self.expected, self.found))
}
}
#[derive(Debug, PartialEq, Eq)]
/// Error indicating value found is outside of a valid range.
pub struct OutOfBounds<T: fmt::Debug> {
@@ -78,6 +112,19 @@ pub struct OutOfBounds<T: fmt::Debug> {
pub found: T,
}
impl<T: fmt::Debug + fmt::Display> fmt::Display for OutOfBounds<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let msg = match (self.min.as_ref(), self.max.as_ref()) {
(Some(min), Some(max)) => format!("Min={}, Max={}", min, max),
(Some(min), _) => format!("Min={}", min),
(_, Some(max)) => format!("Max={}", max),
(None, None) => "".into(),
};
f.write_fmt(format_args!("Value {} out of bounds. {}", self.found, msg))
}
}
impl From<FromHexError> for UtilError {
fn from(err: FromHexError) -> UtilError {
UtilError::FromHex(err)

View File

@@ -56,6 +56,7 @@ mod service;
mod worker;
use mio::{EventLoop, Token};
use std::fmt;
#[derive(Debug)]
/// IO Error
@@ -64,6 +65,16 @@ pub enum IoError {
Mio(::std::io::Error),
}
impl fmt::Display for IoError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// just defer to the std implementation for now.
// we can refine the formatting when more variants are added.
match *self {
IoError::Mio(ref std_err) => std_err.fmt(f)
}
}
}
impl<Message> From<::mio::NotifyError<service::IoMessage<Message>>> for IoError where Message: Send + Clone {
fn from(_err: ::mio::NotifyError<service::IoMessage<Message>>) -> IoError {
IoError::Mio(::std::io::Error::new(::std::io::ErrorKind::ConnectionAborted, "Network IO notification error"))

View File

@@ -17,6 +17,7 @@
use io::IoError;
use crypto::CryptoError;
use rlp::*;
use std::fmt;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum DisconnectReason
@@ -56,6 +57,30 @@ impl DisconnectReason {
}
}
impl fmt::Display for DisconnectReason {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::DisconnectReason::*;
let msg = match *self {
DisconnectRequested => "disconnect requested",
TCPError => "TCP error",
BadProtocol => "bad protocol",
UselessPeer => "useless peer",
TooManyPeers => "too many peers",
DuplicatePeer => "duplicate peer",
IncompatibleProtocol => "incompatible protocol",
NullIdentity => "null identity",
ClientQuit => "client quit",
UnexpectedIdentity => "unexpected identity",
LocalIdentity => "local identity",
PingTimeout => "ping timeout",
Unknown => "unknown",
};
f.write_str(msg)
}
}
#[derive(Debug)]
/// Network error.
pub enum NetworkError {
@@ -73,6 +98,23 @@ pub enum NetworkError {
Io(IoError),
}
impl fmt::Display for NetworkError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use self::NetworkError::*;
let msg = match *self {
Auth => "Authentication failure".into(),
BadProtocol => "Bad protocol".into(),
Expired => "Expired message".into(),
PeerNotFound => "Peer not found".into(),
Disconnect(ref reason) => format!("Peer disconnected: {}", reason),
Io(ref err) => format!("Socket I/O error: {}", err),
};
f.write_fmt(format_args!("Network error ({})", msg))
}
}
impl From<DecoderError> for NetworkError {
fn from(_err: DecoderError) -> NetworkError {
NetworkError::Auth