2018-09-14 10:21:14 +02:00
|
|
|
use std::{net, fmt, error, io, num, str, string};
|
2018-08-10 17:47:02 +02:00
|
|
|
use bs58;
|
|
|
|
use multihash;
|
2018-06-01 10:10:00 +02:00
|
|
|
use byteorder;
|
2018-09-14 10:21:14 +02:00
|
|
|
use unsigned_varint::decode;
|
2018-06-01 10:10:00 +02:00
|
|
|
|
|
|
|
pub type Result<T> = ::std::result::Result<T, Error>;
|
|
|
|
|
|
|
|
/// Error types
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
UnknownProtocol,
|
|
|
|
UnknownProtocolString,
|
|
|
|
InvalidMultiaddr,
|
|
|
|
MissingAddress,
|
|
|
|
ParsingError(Box<error::Error + Send + Sync>),
|
2018-09-14 10:21:14 +02:00
|
|
|
InvalidUvar(decode::Error)
|
2018-06-01 10:10:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-09-14 10:21:14 +02:00
|
|
|
match self {
|
|
|
|
Error::UnknownProtocol => f.write_str("unknown protocol"),
|
|
|
|
Error::UnknownProtocolString => f.write_str("unknown protocol string"),
|
|
|
|
Error::InvalidMultiaddr => f.write_str("invalid multiaddr"),
|
|
|
|
Error::MissingAddress => f.write_str("protocol requires address, none given"),
|
|
|
|
Error::ParsingError(e) => write!(f, "failed to parse: {}", e),
|
|
|
|
Error::InvalidUvar(e) => write!(f, "failed to decode unsigned varint: {}", e)
|
|
|
|
}
|
2018-06-01 10:10:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl error::Error for Error {
|
|
|
|
#[inline]
|
2018-09-14 10:21:14 +02:00
|
|
|
fn cause(&self) -> Option<&dyn error::Error> {
|
|
|
|
if let Error::ParsingError(e) = self {
|
|
|
|
Some(&**e)
|
|
|
|
} else {
|
|
|
|
None
|
2018-06-01 10:10:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(err: io::Error) -> Error {
|
|
|
|
Error::ParsingError(err.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:47:02 +02:00
|
|
|
impl From<multihash::DecodeOwnedError> for Error {
|
|
|
|
fn from(err: multihash::DecodeOwnedError) -> Error {
|
2018-06-01 10:10:00 +02:00
|
|
|
Error::ParsingError(err.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-10 17:47:02 +02:00
|
|
|
impl From<bs58::decode::DecodeError> for Error {
|
|
|
|
fn from(err: bs58::decode::DecodeError) -> Error {
|
|
|
|
Error::ParsingError(err.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-01 10:10:00 +02:00
|
|
|
impl From<net::AddrParseError> for Error {
|
|
|
|
fn from(err: net::AddrParseError) -> Error {
|
|
|
|
Error::ParsingError(err.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<byteorder::Error> for Error {
|
|
|
|
fn from(err: byteorder::Error) -> Error {
|
|
|
|
Error::ParsingError(err.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<num::ParseIntError> for Error {
|
|
|
|
fn from(err: num::ParseIntError) -> Error {
|
|
|
|
Error::ParsingError(err.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<string::FromUtf8Error> for Error {
|
|
|
|
fn from(err: string::FromUtf8Error) -> Error {
|
|
|
|
Error::ParsingError(err.into())
|
|
|
|
}
|
|
|
|
}
|
2018-09-14 10:21:14 +02:00
|
|
|
|
|
|
|
impl From<str::Utf8Error> for Error {
|
|
|
|
fn from(err: str::Utf8Error) -> Error {
|
|
|
|
Error::ParsingError(err.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<decode::Error> for Error {
|
|
|
|
fn from(e: decode::Error) -> Error {
|
|
|
|
Error::InvalidUvar(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|