From a7ee26cd3b8c3e6078f3877f8a8aa470debf61f1 Mon Sep 17 00:00:00 2001 From: Lucio Franco Date: Sat, 21 Sep 2019 16:49:47 -0600 Subject: [PATCH] Use server errorkind --- tonic/src/transport/channel.rs | 1 - tonic/src/transport/error.rs | 65 ++++++++++++++++++++++++++++++++ tonic/src/transport/mod.rs | 68 ++-------------------------------- tonic/src/transport/server.rs | 11 ++++-- 4 files changed, 75 insertions(+), 70 deletions(-) create mode 100644 tonic/src/transport/error.rs diff --git a/tonic/src/transport/channel.rs b/tonic/src/transport/channel.rs index dc7be30..4563038 100644 --- a/tonic/src/transport/channel.rs +++ b/tonic/src/transport/channel.rs @@ -4,7 +4,6 @@ use super::{ }; use crate::{body::BoxBody, client::GrpcService}; use futures_util::try_future::{MapErr, TryFutureExt}; -use http::Uri; use hyper::{Request, Response}; use std::{ convert::TryInto, diff --git a/tonic/src/transport/error.rs b/tonic/src/transport/error.rs new file mode 100644 index 0000000..d057f40 --- /dev/null +++ b/tonic/src/transport/error.rs @@ -0,0 +1,65 @@ +use std::{error, fmt}; + +pub struct Error { + kind: ErrorKind, + source: Option, +} + +#[derive(Debug)] +pub(crate) enum ErrorKind { + Client, + Server, +} + +impl From for Error { + fn from(t: ErrorKind) -> Self { + Self { + kind: t, + source: None, + } + } +} + +impl From<(ErrorKind, crate::Error)> for Error { + fn from(t: (ErrorKind, crate::Error)) -> Self { + Self { + kind: t.0, + source: Some(t.1), + } + } +} + +impl fmt::Debug for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let mut f = f.debug_tuple("Error"); + f.field(&self.kind); + if let Some(source) = &self.source { + f.field(source); + } + f.finish() + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if let Some(source) = &self.source { + write!(f, "{}: {}", self.kind, source) + } else { + write!(f, "{}", self.kind) + } + } +} + +impl error::Error for Error { + fn source(&self) -> Option<&(dyn error::Error + 'static)> { + self.source + .as_ref() + .map(|e| &**e as &(dyn error::Error + 'static)) + } +} + +impl fmt::Display for ErrorKind { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self) + } +} diff --git a/tonic/src/transport/mod.rs b/tonic/src/transport/mod.rs index 9c2b57c..fa367d7 100644 --- a/tonic/src/transport/mod.rs +++ b/tonic/src/transport/mod.rs @@ -4,77 +4,15 @@ mod channel; mod endpoint; +mod error; mod server; mod service; mod tls; pub use self::channel::Channel; pub use self::endpoint::Endpoint; +pub use self::error::Error; pub use self::server::Server; pub use hyper::Body; -use std::{error, fmt}; - -pub struct Error { - kind: ErrorKind, - source: Option, -} - -#[derive(Debug)] -pub(crate) enum ErrorKind { - Client, - // Server, -} - -impl From for Error { - fn from(t: ErrorKind) -> Self { - Self { - kind: t, - source: None, - } - } -} - -impl From<(ErrorKind, crate::Error)> for Error { - fn from(t: (ErrorKind, crate::Error)) -> Self { - Self { - kind: t.0, - source: Some(t.1), - } - } -} - -impl fmt::Debug for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut f = f.debug_tuple("Error"); - f.field(&self.kind); - if let Some(source) = &self.source { - f.field(source); - } - f.finish() - } -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if let Some(source) = &self.source { - write!(f, "{}: {}", self.kind, source) - } else { - write!(f, "{}", self.kind) - } - } -} - -impl error::Error for Error { - fn source(&self) -> Option<&(dyn error::Error + 'static)> { - self.source - .as_ref() - .map(|e| &**e as &(dyn error::Error + 'static)) - } -} - -impl fmt::Display for ErrorKind { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?}", self) - } -} +pub(crate) use self::error::ErrorKind; diff --git a/tonic/src/transport/server.rs b/tonic/src/transport/server.rs index ce1bd10..22bec87 100644 --- a/tonic/src/transport/server.rs +++ b/tonic/src/transport/server.rs @@ -84,7 +84,7 @@ impl Builder { domain: String::new(), }; - Some(TlsAcceptor::new(cert).unwrap()) + Some(TlsAcceptor::new(cert).map_err(map_err)?) } else { None }; @@ -100,12 +100,16 @@ impl Builder { .http2_only(true) .serve(svc) .await - .unwrap(); + .map_err(map_err)?; Ok(()) } } +fn map_err(e: impl Into) -> super::Error { + (super::ErrorKind::Server, e.into()).into() +} + impl fmt::Debug for Builder { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Builder").finish() @@ -201,10 +205,9 @@ where } fn call(&mut self, _: T) -> Self::Future { - // self.inner.make_service(()).map_ok(|s| Svc(s)) let interceptor = self.interceptor.clone(); - // self.inner.make_service(()).map_ok(|s| intercept.layer(BoxService::new(Svc(s)))) let make = self.inner.make_service(()); + Box::pin(async move { let svc = make.await.map_err(Into::into)?;