Use server errorkind

This commit is contained in:
Lucio Franco
2019-09-21 16:49:47 -06:00
parent 1c2905255e
commit a7ee26cd3b
4 changed files with 75 additions and 70 deletions
-1
View File
@@ -4,7 +4,6 @@ use super::{
}; };
use crate::{body::BoxBody, client::GrpcService}; use crate::{body::BoxBody, client::GrpcService};
use futures_util::try_future::{MapErr, TryFutureExt}; use futures_util::try_future::{MapErr, TryFutureExt};
use http::Uri;
use hyper::{Request, Response}; use hyper::{Request, Response};
use std::{ use std::{
convert::TryInto, convert::TryInto,
+65
View File
@@ -0,0 +1,65 @@
use std::{error, fmt};
pub struct Error {
kind: ErrorKind,
source: Option<crate::Error>,
}
#[derive(Debug)]
pub(crate) enum ErrorKind {
Client,
Server,
}
impl From<ErrorKind> 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)
}
}
+3 -65
View File
@@ -4,77 +4,15 @@
mod channel; mod channel;
mod endpoint; mod endpoint;
mod error;
mod server; mod server;
mod service; mod service;
mod tls; mod tls;
pub use self::channel::Channel; pub use self::channel::Channel;
pub use self::endpoint::Endpoint; pub use self::endpoint::Endpoint;
pub use self::error::Error;
pub use self::server::Server; pub use self::server::Server;
pub use hyper::Body; pub use hyper::Body;
use std::{error, fmt}; pub(crate) use self::error::ErrorKind;
pub struct Error {
kind: ErrorKind,
source: Option<crate::Error>,
}
#[derive(Debug)]
pub(crate) enum ErrorKind {
Client,
// Server,
}
impl From<ErrorKind> 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)
}
}
+7 -4
View File
@@ -84,7 +84,7 @@ impl Builder {
domain: String::new(), domain: String::new(),
}; };
Some(TlsAcceptor::new(cert).unwrap()) Some(TlsAcceptor::new(cert).map_err(map_err)?)
} else { } else {
None None
}; };
@@ -100,12 +100,16 @@ impl Builder {
.http2_only(true) .http2_only(true)
.serve(svc) .serve(svc)
.await .await
.unwrap(); .map_err(map_err)?;
Ok(()) Ok(())
} }
} }
fn map_err(e: impl Into<crate::Error>) -> super::Error {
(super::ErrorKind::Server, e.into()).into()
}
impl fmt::Debug for Builder { impl fmt::Debug for Builder {
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()
@@ -201,10 +205,9 @@ where
} }
fn call(&mut self, _: T) -> Self::Future { fn call(&mut self, _: T) -> Self::Future {
// self.inner.make_service(()).map_ok(|s| Svc(s))
let interceptor = self.interceptor.clone(); let interceptor = self.interceptor.clone();
// self.inner.make_service(()).map_ok(|s| intercept.layer(BoxService::new(Svc(s))))
let make = self.inner.make_service(()); let make = self.inner.make_service(());
Box::pin(async move { Box::pin(async move {
let svc = make.await.map_err(Into::into)?; let svc = make.await.map_err(Into::into)?;