Skip to content

Commit 858d655

Browse files
committed
Make transport error simpler and just proxy for the source error
1 parent 49ce265 commit 858d655

File tree

5 files changed

+15
-58
lines changed

5 files changed

+15
-58
lines changed

tonic/src/transport/channel/endpoint.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::Channel;
44
use super::ClientTlsConfig;
55
#[cfg(feature = "tls")]
66
use crate::transport::service::TlsConnector;
7-
use crate::transport::{Error, ErrorKind};
7+
use crate::transport::Error;
88
use bytes::Bytes;
99
use http::uri::{InvalidUri, Uri};
1010
use std::{
@@ -44,9 +44,7 @@ impl Endpoint {
4444
D: TryInto<Self>,
4545
D::Error: Into<crate::Error>,
4646
{
47-
let me = dst
48-
.try_into()
49-
.map_err(|e| Error::from_source(ErrorKind::Client, e.into()))?;
47+
let me = dst.try_into().map_err(|e| Error::from_source(e.into()))?;
5048
Ok(me)
5149
}
5250

tonic/src/transport/channel/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl Channel {
136136

137137
let svc = Connection::new(connector, endpoint)
138138
.await
139-
.map_err(|e| super::Error::from_source(super::ErrorKind::Client, e))?;
139+
.map_err(|e| super::Error::from_source(e))?;
140140

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

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

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

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

194193
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
195194
let val = futures_util::ready!(Pin::new(&mut self.inner).poll(cx))
196-
.map_err(|e| super::Error::from_source(super::ErrorKind::Client, e))?;
195+
.map_err(|e| super::Error::from_source(e))?;
197196
Ok(val).into()
198197
}
199198
}

tonic/src/transport/error.rs

+6-40
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,23 @@
11
use std::{error, fmt};
22

33
/// Error's that originate from the client or server;
4-
pub struct Error {
5-
kind: ErrorKind,
6-
source: Option<crate::Error>,
7-
}
8-
9-
impl Error {
10-
pub(crate) fn from_source(kind: ErrorKind, source: crate::Error) -> Self {
11-
Self {
12-
kind,
13-
source: Some(source),
14-
}
15-
}
16-
}
17-
184
#[derive(Debug)]
19-
pub(crate) enum ErrorKind {
20-
Client,
21-
Server,
22-
}
5+
pub struct Error(crate::Error);
236

24-
impl fmt::Debug for Error {
25-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26-
let mut f = f.debug_tuple("Error");
27-
f.field(&self.kind);
28-
if let Some(source) = &self.source {
29-
f.field(source);
30-
}
31-
f.finish()
7+
impl Error {
8+
pub(crate) fn from_source(source: impl Into<crate::Error>) -> Self {
9+
Self(source.into())
3210
}
3311
}
3412

3513
impl fmt::Display for Error {
3614
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37-
if let Some(source) = &self.source {
38-
write!(f, "{}: {}", self.kind, source)
39-
} else {
40-
write!(f, "{}", self.kind)
41-
}
15+
self.0.fmt(f)
4216
}
4317
}
4418

4519
impl error::Error for Error {
4620
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
47-
self.source
48-
.as_ref()
49-
.map(|e| &**e as &(dyn error::Error + 'static))
50-
}
51-
}
52-
53-
impl fmt::Display for ErrorKind {
54-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55-
write!(f, "{:?}", self)
21+
self.0.source()
5622
}
5723
}

tonic/src/transport/mod.rs

-2
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,3 @@ pub use self::channel::ClientTlsConfig;
112112
#[cfg(feature = "tls")]
113113
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
114114
pub use self::server::ServerTlsConfig;
115-
116-
pub(crate) use self::error::ErrorKind;

tonic/src/transport/server/mod.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,9 @@ impl Server {
300300
.serve(svc)
301301
.with_graceful_shutdown(signal)
302302
.await
303-
.map_err(map_err)?
303+
.map_err(super::Error::from_source)?
304304
} else {
305-
server.serve(svc).await.map_err(map_err)?;
305+
server.serve(svc).await.map_err(super::Error::from_source)?;
306306
}
307307

308308
Ok(())
@@ -374,7 +374,7 @@ where
374374
/// [`Server`]: struct.Server.html
375375
pub async fn serve(self, addr: SocketAddr) -> Result<(), super::Error> {
376376
let incoming = TcpIncoming::new(addr, self.server.tcp_nodelay, self.server.tcp_keepalive)
377-
.map_err(map_err)?;
377+
.map_err(super::Error::from_source)?;
378378
self.server
379379
.serve_with_shutdown::<_, _, future::Ready<()>, _, _>(self.routes, incoming, None)
380380
.await
@@ -391,7 +391,7 @@ where
391391
f: F,
392392
) -> Result<(), super::Error> {
393393
let incoming = TcpIncoming::new(addr, self.server.tcp_nodelay, self.server.tcp_keepalive)
394-
.map_err(map_err)?;
394+
.map_err(super::Error::from_source)?;
395395
self.server
396396
.serve_with_shutdown(self.routes, incoming, Some(f))
397397
.await
@@ -413,10 +413,6 @@ where
413413
}
414414
}
415415

416-
fn map_err(e: impl Into<crate::Error>) -> super::Error {
417-
super::Error::from_source(super::ErrorKind::Server, e.into())
418-
}
419-
420416
impl fmt::Debug for Server {
421417
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
422418
f.debug_struct("Builder").finish()

0 commit comments

Comments
 (0)