RunError and few more error types implements Error (#501)

This allows them to be used with things like `failure`.
This commit is contained in:
Michal 'vorner' Vaner 2018-07-24 22:27:57 +02:00 committed by Carl Lerche
parent 365efec24a
commit 84db325628
2 changed files with 70 additions and 0 deletions

View File

@ -9,6 +9,8 @@ use tokio_executor;
use futures::Future; use futures::Future;
use std::fmt;
use std::error::Error;
use std::io; use std::io;
/// Single-threaded runtime provides a way to start reactor /// Single-threaded runtime provides a way to start reactor
@ -48,6 +50,21 @@ pub struct RunError {
inner: current_thread::RunError, inner: current_thread::RunError,
} }
impl fmt::Display for RunError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", self.inner)
}
}
impl Error for RunError {
fn description(&self) -> &str {
self.inner.description()
}
fn cause(&self) -> Option<&Error> {
self.inner.cause()
}
}
impl Runtime { impl Runtime {
/// Returns a new runtime initialized with default configuration values. /// Returns a new runtime initialized with default configuration values.
pub fn new() -> io::Result<Runtime> { pub fn new() -> io::Result<Runtime> {

View File

@ -40,6 +40,7 @@ use futures::future::{Executor, ExecuteError, ExecuteErrorKind};
use std::fmt; use std::fmt;
use std::cell::Cell; use std::cell::Cell;
use std::error::Error;
use std::rc::Rc; use std::rc::Rc;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use std::sync::mpsc; use std::sync::mpsc;
@ -103,24 +104,76 @@ pub struct RunError {
_p: (), _p: (),
} }
impl fmt::Display for RunError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", self.description())
}
}
impl Error for RunError {
fn description(&self) -> &str {
"Run error"
}
}
/// Error returned by the `run_timeout` function. /// Error returned by the `run_timeout` function.
#[derive(Debug)] #[derive(Debug)]
pub struct RunTimeoutError { pub struct RunTimeoutError {
timeout: bool, timeout: bool,
} }
impl fmt::Display for RunTimeoutError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", self.description())
}
}
impl Error for RunTimeoutError {
fn description(&self) -> &str {
if self.timeout {
"Run timeout error (timeout)"
} else {
"Run timeout error (not timeout)"
}
}
}
/// Error returned by the `turn` function. /// Error returned by the `turn` function.
#[derive(Debug)] #[derive(Debug)]
pub struct TurnError { pub struct TurnError {
_p: (), _p: (),
} }
impl fmt::Display for TurnError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", self.description())
}
}
impl Error for TurnError {
fn description(&self) -> &str {
"Turn error"
}
}
/// Error returned by the `block_on` function. /// Error returned by the `block_on` function.
#[derive(Debug)] #[derive(Debug)]
pub struct BlockError<T> { pub struct BlockError<T> {
inner: Option<T>, inner: Option<T>,
} }
impl<T> fmt::Display for BlockError<T> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "Block error")
}
}
impl<T: fmt::Debug> Error for BlockError<T> {
fn description(&self) -> &str {
"Block error"
}
}
/// This is mostly split out to make the borrow checker happy. /// This is mostly split out to make the borrow checker happy.
struct Borrow<'a, U: 'a> { struct Borrow<'a, U: 'a> {
scheduler: &'a mut Scheduler<U>, scheduler: &'a mut Scheduler<U>,