2018-03-30 10:43:38 -07:00
|
|
|
//! The `result` module exposes a Result type that propagates one of many different Error types.
|
2018-03-29 11:20:54 -07:00
|
|
|
|
2019-02-07 20:52:39 -08:00
|
|
|
use crate::blocktree;
|
2018-12-07 19:16:27 -08:00
|
|
|
use crate::cluster_info;
|
2018-12-08 21:52:29 -08:00
|
|
|
#[cfg(feature = "erasure")]
|
|
|
|
use crate::erasure;
|
2018-12-07 19:16:27 -08:00
|
|
|
use crate::packet;
|
|
|
|
use crate::poh_recorder;
|
2018-03-26 21:03:26 -07:00
|
|
|
use bincode;
|
2018-03-07 13:47:13 -08:00
|
|
|
use serde_json;
|
2019-02-18 22:26:22 -08:00
|
|
|
use solana_runtime::bank;
|
2018-03-07 13:47:13 -08:00
|
|
|
use std;
|
|
|
|
use std::any::Any;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
IO(std::io::Error),
|
|
|
|
JSON(serde_json::Error),
|
|
|
|
AddrParse(std::net::AddrParseError),
|
2018-12-08 21:44:20 -08:00
|
|
|
JoinError(Box<dyn Any + Send + 'static>),
|
2018-03-07 13:47:13 -08:00
|
|
|
RecvError(std::sync::mpsc::RecvError),
|
|
|
|
RecvTimeoutError(std::sync::mpsc::RecvTimeoutError),
|
2019-02-19 16:17:36 -08:00
|
|
|
TryRecvError(std::sync::mpsc::TryRecvError),
|
2018-03-10 20:09:17 -08:00
|
|
|
Serialize(std::boxed::Box<bincode::ErrorKind>),
|
2018-05-14 14:33:11 -07:00
|
|
|
BankError(bank::BankError),
|
2018-10-08 19:55:54 -07:00
|
|
|
ClusterInfoError(cluster_info::ClusterInfoError),
|
2018-07-25 12:15:26 -07:00
|
|
|
BlobError(packet::BlobError),
|
2018-07-05 13:37:13 -07:00
|
|
|
#[cfg(feature = "erasure")]
|
|
|
|
ErasureError(erasure::ErasureError),
|
2018-03-07 13:47:13 -08:00
|
|
|
SendError,
|
2018-10-18 22:57:48 -07:00
|
|
|
PohRecorderError(poh_recorder::PohRecorderError),
|
2019-02-07 20:52:39 -08:00
|
|
|
BlocktreeError(blocktree::BlocktreeError),
|
2018-03-07 13:47:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
2018-08-20 13:03:36 -07:00
|
|
|
impl std::fmt::Display for Error {
|
2018-12-08 21:44:20 -08:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2018-08-20 13:03:36 -07:00
|
|
|
write!(f, "solana error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for Error {}
|
|
|
|
|
2018-03-07 13:47:13 -08:00
|
|
|
impl std::convert::From<std::sync::mpsc::RecvError> for Error {
|
|
|
|
fn from(e: std::sync::mpsc::RecvError) -> Error {
|
|
|
|
Error::RecvError(e)
|
|
|
|
}
|
|
|
|
}
|
2019-02-19 16:17:36 -08:00
|
|
|
impl std::convert::From<std::sync::mpsc::TryRecvError> for Error {
|
|
|
|
fn from(e: std::sync::mpsc::TryRecvError) -> Error {
|
|
|
|
Error::TryRecvError(e)
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 13:47:13 -08:00
|
|
|
impl std::convert::From<std::sync::mpsc::RecvTimeoutError> for Error {
|
|
|
|
fn from(e: std::sync::mpsc::RecvTimeoutError) -> Error {
|
|
|
|
Error::RecvTimeoutError(e)
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 14:33:11 -07:00
|
|
|
impl std::convert::From<bank::BankError> for Error {
|
|
|
|
fn from(e: bank::BankError) -> Error {
|
|
|
|
Error::BankError(e)
|
2018-04-19 10:32:02 -07:00
|
|
|
}
|
|
|
|
}
|
2018-10-08 19:55:54 -07:00
|
|
|
impl std::convert::From<cluster_info::ClusterInfoError> for Error {
|
|
|
|
fn from(e: cluster_info::ClusterInfoError) -> Error {
|
|
|
|
Error::ClusterInfoError(e)
|
2018-07-05 13:37:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(feature = "erasure")]
|
|
|
|
impl std::convert::From<erasure::ErasureError> for Error {
|
|
|
|
fn from(e: erasure::ErasureError) -> Error {
|
|
|
|
Error::ErasureError(e)
|
|
|
|
}
|
|
|
|
}
|
2018-03-07 13:47:13 -08:00
|
|
|
impl<T> std::convert::From<std::sync::mpsc::SendError<T>> for Error {
|
|
|
|
fn from(_e: std::sync::mpsc::SendError<T>) -> Error {
|
|
|
|
Error::SendError
|
|
|
|
}
|
|
|
|
}
|
2018-12-08 21:44:20 -08:00
|
|
|
impl std::convert::From<Box<dyn Any + Send + 'static>> for Error {
|
|
|
|
fn from(e: Box<dyn Any + Send + 'static>) -> Error {
|
2018-03-07 13:47:13 -08:00
|
|
|
Error::JoinError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::convert::From<std::io::Error> for Error {
|
|
|
|
fn from(e: std::io::Error) -> Error {
|
|
|
|
Error::IO(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::convert::From<serde_json::Error> for Error {
|
|
|
|
fn from(e: serde_json::Error) -> Error {
|
|
|
|
Error::JSON(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::convert::From<std::net::AddrParseError> for Error {
|
|
|
|
fn from(e: std::net::AddrParseError) -> Error {
|
|
|
|
Error::AddrParse(e)
|
|
|
|
}
|
|
|
|
}
|
2018-03-10 20:09:17 -08:00
|
|
|
impl std::convert::From<std::boxed::Box<bincode::ErrorKind>> for Error {
|
|
|
|
fn from(e: std::boxed::Box<bincode::ErrorKind>) -> Error {
|
|
|
|
Error::Serialize(e)
|
|
|
|
}
|
|
|
|
}
|
2018-10-18 22:57:48 -07:00
|
|
|
impl std::convert::From<poh_recorder::PohRecorderError> for Error {
|
|
|
|
fn from(e: poh_recorder::PohRecorderError) -> Error {
|
|
|
|
Error::PohRecorderError(e)
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 20:52:39 -08:00
|
|
|
impl std::convert::From<blocktree::BlocktreeError> for Error {
|
|
|
|
fn from(e: blocktree::BlocktreeError) -> Error {
|
|
|
|
Error::BlocktreeError(e)
|
2018-11-15 15:53:31 -08:00
|
|
|
}
|
|
|
|
}
|
2018-03-07 13:47:13 -08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-12-07 19:16:27 -08:00
|
|
|
use crate::result::Error;
|
|
|
|
use crate::result::Result;
|
2018-03-26 21:03:26 -07:00
|
|
|
use serde_json;
|
|
|
|
use std::io;
|
|
|
|
use std::io::Write;
|
2018-03-07 13:47:13 -08:00
|
|
|
use std::net::SocketAddr;
|
2018-05-14 08:05:29 -07:00
|
|
|
use std::panic;
|
2018-05-29 20:20:28 -07:00
|
|
|
use std::sync::mpsc::channel;
|
2018-03-07 13:47:13 -08:00
|
|
|
use std::sync::mpsc::RecvError;
|
|
|
|
use std::sync::mpsc::RecvTimeoutError;
|
2018-03-26 21:03:26 -07:00
|
|
|
use std::thread;
|
2018-03-07 13:47:13 -08:00
|
|
|
|
|
|
|
fn addr_parse_error() -> Result<SocketAddr> {
|
|
|
|
let r = "12fdfasfsafsadfs".parse()?;
|
|
|
|
Ok(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn join_error() -> Result<()> {
|
2018-05-14 08:05:29 -07:00
|
|
|
panic::set_hook(Box::new(|_info| {}));
|
2018-03-07 13:47:13 -08:00
|
|
|
let r = thread::spawn(|| panic!("hi")).join()?;
|
|
|
|
Ok(r)
|
|
|
|
}
|
|
|
|
fn json_error() -> Result<()> {
|
|
|
|
let r = serde_json::from_slice("=342{;;;;:}".as_bytes())?;
|
|
|
|
Ok(r)
|
|
|
|
}
|
|
|
|
fn send_error() -> Result<()> {
|
|
|
|
let (s, r) = channel();
|
|
|
|
drop(r);
|
|
|
|
s.send(())?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_test() {
|
|
|
|
assert_matches!(addr_parse_error(), Err(Error::AddrParse(_)));
|
|
|
|
assert_matches!(Error::from(RecvError {}), Error::RecvError(_));
|
|
|
|
assert_matches!(
|
|
|
|
Error::from(RecvTimeoutError::Timeout),
|
|
|
|
Error::RecvTimeoutError(_)
|
|
|
|
);
|
|
|
|
assert_matches!(send_error(), Err(Error::SendError));
|
|
|
|
assert_matches!(join_error(), Err(Error::JoinError(_)));
|
|
|
|
let ioe = io::Error::new(io::ErrorKind::NotFound, "hi");
|
|
|
|
assert_matches!(Error::from(ioe), Error::IO(_));
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn fmt_test() {
|
|
|
|
write!(io::sink(), "{:?}", addr_parse_error()).unwrap();
|
|
|
|
write!(io::sink(), "{:?}", Error::from(RecvError {})).unwrap();
|
|
|
|
write!(io::sink(), "{:?}", Error::from(RecvTimeoutError::Timeout)).unwrap();
|
|
|
|
write!(io::sink(), "{:?}", send_error()).unwrap();
|
|
|
|
write!(io::sink(), "{:?}", join_error()).unwrap();
|
|
|
|
write!(io::sink(), "{:?}", json_error()).unwrap();
|
|
|
|
write!(
|
|
|
|
io::sink(),
|
|
|
|
"{:?}",
|
|
|
|
Error::from(io::Error::new(io::ErrorKind::NotFound, "hi"))
|
2018-12-07 19:01:28 -08:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-03-07 13:47:13 -08:00
|
|
|
}
|
|
|
|
}
|