Skip to content

Commit

Permalink
fix(transport): Improve Error type (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
faern authored and LucioFranco committed Jan 4, 2020
1 parent 49ce265 commit ec1f37e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 58 deletions.
6 changes: 2 additions & 4 deletions tonic/src/transport/channel/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::Channel;
use super::ClientTlsConfig;
#[cfg(feature = "tls")]
use crate::transport::service::TlsConnector;
use crate::transport::{Error, ErrorKind};
use crate::transport::Error;
use bytes::Bytes;
use http::uri::{InvalidUri, Uri};
use std::{
Expand Down Expand Up @@ -44,9 +44,7 @@ impl Endpoint {
D: TryInto<Self>,
D::Error: Into<crate::Error>,
{
let me = dst
.try_into()
.map_err(|e| Error::from_source(ErrorKind::Client, e.into()))?;
let me = dst.try_into().map_err(|e| Error::from_source(e.into()))?;
Ok(me)
}

Expand Down
7 changes: 3 additions & 4 deletions tonic/src/transport/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Channel {

let svc = Connection::new(connector, endpoint)
.await
.map_err(|e| super::Error::from_source(super::ErrorKind::Client, e))?;
.map_err(|e| super::Error::from_source(e))?;

let svc = Buffer::new(Either::A(svc), buffer_size);

Expand Down Expand Up @@ -174,8 +174,7 @@ impl GrpcService<BoxBody> for Channel {
type Future = ResponseFuture;

fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
GrpcService::poll_ready(&mut self.svc, cx)
.map_err(|e| super::Error::from_source(super::ErrorKind::Client, e))
GrpcService::poll_ready(&mut self.svc, cx).map_err(|e| super::Error::from_source(e))
}

fn call(&mut self, mut request: Request<BoxBody>) -> Self::Future {
Expand All @@ -193,7 +192,7 @@ impl Future for ResponseFuture {

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let val = futures_util::ready!(Pin::new(&mut self.inner).poll(cx))
.map_err(|e| super::Error::from_source(super::ErrorKind::Client, e))?;
.map_err(|e| super::Error::from_source(e))?;
Ok(val).into()
}
}
Expand Down
46 changes: 6 additions & 40 deletions tonic/src/transport/error.rs
Original file line number Diff line number Diff line change
@@ -1,57 +1,23 @@
use std::{error, fmt};

/// Error's that originate from the client or server;
pub struct Error {
kind: ErrorKind,
source: Option<crate::Error>,
}

impl Error {
pub(crate) fn from_source(kind: ErrorKind, source: crate::Error) -> Self {
Self {
kind,
source: Some(source),
}
}
}

#[derive(Debug)]
pub(crate) enum ErrorKind {
Client,
Server,
}
pub struct Error(crate::Error);

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 Error {
pub(crate) fn from_source(source: impl Into<crate::Error>) -> Self {
Self(source.into())
}
}

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)
}
self.0.fmt(f)
}
}

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)
self.0.source()
}
}
2 changes: 0 additions & 2 deletions tonic/src/transport/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,3 @@ pub use self::channel::ClientTlsConfig;
#[cfg(feature = "tls")]
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
pub use self::server::ServerTlsConfig;

pub(crate) use self::error::ErrorKind;
12 changes: 4 additions & 8 deletions tonic/src/transport/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,9 @@ impl Server {
.serve(svc)
.with_graceful_shutdown(signal)
.await
.map_err(map_err)?
.map_err(super::Error::from_source)?
} else {
server.serve(svc).await.map_err(map_err)?;
server.serve(svc).await.map_err(super::Error::from_source)?;
}

Ok(())
Expand Down Expand Up @@ -374,7 +374,7 @@ where
/// [`Server`]: struct.Server.html
pub async fn serve(self, addr: SocketAddr) -> Result<(), super::Error> {
let incoming = TcpIncoming::new(addr, self.server.tcp_nodelay, self.server.tcp_keepalive)
.map_err(map_err)?;
.map_err(super::Error::from_source)?;
self.server
.serve_with_shutdown::<_, _, future::Ready<()>, _, _>(self.routes, incoming, None)
.await
Expand All @@ -391,7 +391,7 @@ where
f: F,
) -> Result<(), super::Error> {
let incoming = TcpIncoming::new(addr, self.server.tcp_nodelay, self.server.tcp_keepalive)
.map_err(map_err)?;
.map_err(super::Error::from_source)?;
self.server
.serve_with_shutdown(self.routes, incoming, Some(f))
.await
Expand All @@ -413,10 +413,6 @@ where
}
}

fn map_err(e: impl Into<crate::Error>) -> super::Error {
super::Error::from_source(super::ErrorKind::Server, e.into())
}

impl fmt::Debug for Server {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Builder").finish()
Expand Down

0 comments on commit ec1f37e

Please sign in to comment.