fix(transport): Improve Error type (#217)
This commit is contained in:
committed by
Lucio Franco
parent
49ce265954
commit
ec1f37e4b4
@@ -4,7 +4,7 @@ use super::Channel;
|
|||||||
use super::ClientTlsConfig;
|
use super::ClientTlsConfig;
|
||||||
#[cfg(feature = "tls")]
|
#[cfg(feature = "tls")]
|
||||||
use crate::transport::service::TlsConnector;
|
use crate::transport::service::TlsConnector;
|
||||||
use crate::transport::{Error, ErrorKind};
|
use crate::transport::Error;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use http::uri::{InvalidUri, Uri};
|
use http::uri::{InvalidUri, Uri};
|
||||||
use std::{
|
use std::{
|
||||||
@@ -44,9 +44,7 @@ impl Endpoint {
|
|||||||
D: TryInto<Self>,
|
D: TryInto<Self>,
|
||||||
D::Error: Into<crate::Error>,
|
D::Error: Into<crate::Error>,
|
||||||
{
|
{
|
||||||
let me = dst
|
let me = dst.try_into().map_err(|e| Error::from_source(e.into()))?;
|
||||||
.try_into()
|
|
||||||
.map_err(|e| Error::from_source(ErrorKind::Client, e.into()))?;
|
|
||||||
Ok(me)
|
Ok(me)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ impl Channel {
|
|||||||
|
|
||||||
let svc = Connection::new(connector, endpoint)
|
let svc = Connection::new(connector, endpoint)
|
||||||
.await
|
.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);
|
let svc = Buffer::new(Either::A(svc), buffer_size);
|
||||||
|
|
||||||
@@ -174,8 +174,7 @@ impl GrpcService<BoxBody> for Channel {
|
|||||||
type Future = ResponseFuture;
|
type Future = ResponseFuture;
|
||||||
|
|
||||||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||||
GrpcService::poll_ready(&mut self.svc, cx)
|
GrpcService::poll_ready(&mut self.svc, cx).map_err(|e| super::Error::from_source(e))
|
||||||
.map_err(|e| super::Error::from_source(super::ErrorKind::Client, e))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn call(&mut self, mut request: Request<BoxBody>) -> Self::Future {
|
fn call(&mut self, mut request: Request<BoxBody>) -> Self::Future {
|
||||||
@@ -193,7 +192,7 @@ impl Future for ResponseFuture {
|
|||||||
|
|
||||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
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))
|
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()
|
Ok(val).into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,57 +1,23 @@
|
|||||||
use std::{error, fmt};
|
use std::{error, fmt};
|
||||||
|
|
||||||
/// Error's that originate from the client or server;
|
/// Error's that originate from the client or server;
|
||||||
pub struct Error {
|
#[derive(Debug)]
|
||||||
kind: ErrorKind,
|
pub struct Error(crate::Error);
|
||||||
source: Option<crate::Error>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Error {
|
impl Error {
|
||||||
pub(crate) fn from_source(kind: ErrorKind, source: crate::Error) -> Self {
|
pub(crate) fn from_source(source: impl Into<crate::Error>) -> Self {
|
||||||
Self {
|
Self(source.into())
|
||||||
kind,
|
|
||||||
source: Some(source),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub(crate) enum ErrorKind {
|
|
||||||
Client,
|
|
||||||
Server,
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
impl fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
if let Some(source) = &self.source {
|
self.0.fmt(f)
|
||||||
write!(f, "{}: {}", self.kind, source)
|
|
||||||
} else {
|
|
||||||
write!(f, "{}", self.kind)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl error::Error for Error {
|
impl error::Error for Error {
|
||||||
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
||||||
self.source
|
self.0.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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,5 +112,3 @@ pub use self::channel::ClientTlsConfig;
|
|||||||
#[cfg(feature = "tls")]
|
#[cfg(feature = "tls")]
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
|
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
|
||||||
pub use self::server::ServerTlsConfig;
|
pub use self::server::ServerTlsConfig;
|
||||||
|
|
||||||
pub(crate) use self::error::ErrorKind;
|
|
||||||
|
|||||||
@@ -300,9 +300,9 @@ impl Server {
|
|||||||
.serve(svc)
|
.serve(svc)
|
||||||
.with_graceful_shutdown(signal)
|
.with_graceful_shutdown(signal)
|
||||||
.await
|
.await
|
||||||
.map_err(map_err)?
|
.map_err(super::Error::from_source)?
|
||||||
} else {
|
} else {
|
||||||
server.serve(svc).await.map_err(map_err)?;
|
server.serve(svc).await.map_err(super::Error::from_source)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -374,7 +374,7 @@ where
|
|||||||
/// [`Server`]: struct.Server.html
|
/// [`Server`]: struct.Server.html
|
||||||
pub async fn serve(self, addr: SocketAddr) -> Result<(), super::Error> {
|
pub async fn serve(self, addr: SocketAddr) -> Result<(), super::Error> {
|
||||||
let incoming = TcpIncoming::new(addr, self.server.tcp_nodelay, self.server.tcp_keepalive)
|
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
|
self.server
|
||||||
.serve_with_shutdown::<_, _, future::Ready<()>, _, _>(self.routes, incoming, None)
|
.serve_with_shutdown::<_, _, future::Ready<()>, _, _>(self.routes, incoming, None)
|
||||||
.await
|
.await
|
||||||
@@ -391,7 +391,7 @@ where
|
|||||||
f: F,
|
f: F,
|
||||||
) -> Result<(), super::Error> {
|
) -> Result<(), super::Error> {
|
||||||
let incoming = TcpIncoming::new(addr, self.server.tcp_nodelay, self.server.tcp_keepalive)
|
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
|
self.server
|
||||||
.serve_with_shutdown(self.routes, incoming, Some(f))
|
.serve_with_shutdown(self.routes, incoming, Some(f))
|
||||||
.await
|
.await
|
||||||
@@ -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 {
|
impl fmt::Debug for Server {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
f.debug_struct("Builder").finish()
|
f.debug_struct("Builder").finish()
|
||||||
|
|||||||
Reference in New Issue
Block a user