From 84db3256284e2cd9db18864666fd76e490f944ac Mon Sep 17 00:00:00 2001 From: Michal 'vorner' Vaner Date: Tue, 24 Jul 2018 22:27:57 +0200 Subject: [PATCH] RunError and few more error types implements Error (#501) This allows them to be used with things like `failure`. --- src/runtime/current_thread/runtime.rs | 17 +++++++++ tokio-current-thread/src/lib.rs | 53 +++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/src/runtime/current_thread/runtime.rs b/src/runtime/current_thread/runtime.rs index 365c27faa..7df915d37 100644 --- a/src/runtime/current_thread/runtime.rs +++ b/src/runtime/current_thread/runtime.rs @@ -9,6 +9,8 @@ use tokio_executor; use futures::Future; +use std::fmt; +use std::error::Error; use std::io; /// Single-threaded runtime provides a way to start reactor @@ -48,6 +50,21 @@ pub struct 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 { /// Returns a new runtime initialized with default configuration values. pub fn new() -> io::Result { diff --git a/tokio-current-thread/src/lib.rs b/tokio-current-thread/src/lib.rs index 758878d59..036893b5c 100644 --- a/tokio-current-thread/src/lib.rs +++ b/tokio-current-thread/src/lib.rs @@ -40,6 +40,7 @@ use futures::future::{Executor, ExecuteError, ExecuteErrorKind}; use std::fmt; use std::cell::Cell; +use std::error::Error; use std::rc::Rc; use std::time::{Duration, Instant}; use std::sync::mpsc; @@ -103,24 +104,76 @@ pub struct RunError { _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. #[derive(Debug)] pub struct RunTimeoutError { 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. #[derive(Debug)] pub struct TurnError { _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. #[derive(Debug)] pub struct BlockError { inner: Option, } +impl fmt::Display for BlockError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "Block error") + } +} + +impl Error for BlockError { + fn description(&self) -> &str { + "Block error" + } +} + /// This is mostly split out to make the borrow checker happy. struct Borrow<'a, U: 'a> { scheduler: &'a mut Scheduler,