refactor(error): improve error message when tokio::spawn fails

Closes #1635
This commit is contained in:
Sean McArthur
2018-10-16 15:14:06 -07:00
parent 12bc4cb485
commit c5e807715a
2 changed files with 36 additions and 10 deletions

View File

@@ -1,3 +1,4 @@
use std::error::Error as StdError;
use std::fmt;
use std::sync::Arc;
@@ -40,7 +41,7 @@ impl Exec {
.spawn(Box::new(fut))
.map_err(|err| {
warn!("executor error: {:?}", err);
::Error::new_execute()
::Error::new_execute(TokioSpawnError)
})
}
#[cfg(not(feature = "runtime"))]
@@ -53,7 +54,7 @@ impl Exec {
e.execute(Box::new(fut))
.map_err(|err| {
warn!("executor error: {:?}", err.kind());
::Error::new_execute()
::Error::new_execute("custom executor failed")
})
},
}
@@ -101,7 +102,7 @@ where
self.execute(fut)
.map_err(|err| {
warn!("executor error: {:?}", err.kind());
::Error::new_execute()
::Error::new_execute("custom executor failed")
})
}
}
@@ -117,7 +118,30 @@ where
self.execute(fut)
.map_err(|err| {
warn!("executor error: {:?}", err.kind());
::Error::new_execute()
::Error::new_execute("custom executor failed")
})
}
}
// ===== StdError impls =====
struct TokioSpawnError;
impl fmt::Debug for TokioSpawnError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt("tokio::spawn failed (is a tokio runtime running this future?)", f)
}
}
impl fmt::Display for TokioSpawnError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt("tokio::spawn failed (is a tokio runtime running this future?)", f)
}
}
impl StdError for TokioSpawnError {
fn description(&self) -> &str {
"tokio::spawn failed"
}
}

View File

@@ -258,8 +258,8 @@ impl Error {
Error::new(Kind::Shutdown, Some(Box::new(cause)))
}
pub(crate) fn new_execute() -> Error {
Error::new(Kind::Execute, None)
pub(crate) fn new_execute<E: Into<Cause>>(cause: E) -> Error {
Error::new(Kind::Execute, Some(cause.into()))
}
pub(crate) fn new_h2(cause: ::h2::Error) -> Error {
@@ -269,10 +269,12 @@ impl Error {
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Error")
.field("kind", &self.inner.kind)
.field("cause", &self.inner.cause)
.finish()
let mut f = f.debug_struct("Error");
f.field("kind", &self.inner.kind);
if let Some(ref cause) = self.inner.cause {
f.field("cause", cause);
}
f.finish()
}
}