Use server errorkind
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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<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)
|
||||
}
|
||||
}
|
||||
pub(crate) use self::error::ErrorKind;
|
||||
|
||||
@@ -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<crate::Error>) -> 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)?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user