use crate::Error as TransportError;
use std::ops::ControlFlow;
use std::{error::Error as StdError, fmt, io};
use tracing::debug;
pub(crate) fn handle_accept_error(e: impl Into<TransportError>) -> ControlFlow<TransportError> {
let e = e.into();
debug!(error = %e, "connection accept loop error");
if let Some(e) = e.downcast_ref::<io::Error>() {
if matches!(
e.kind(),
io::ErrorKind::ConnectionAborted | io::ErrorKind::Interrupted | io::ErrorKind::InvalidData | io::ErrorKind::WouldBlock ) {
return ControlFlow::Continue(());
}
}
ControlFlow::Break(e)
}
type Source = crate::Error;
pub struct Error {
inner: ErrorImpl,
}
struct ErrorImpl {
kind: Kind,
source: Option<Source>,
}
#[derive(Debug)]
pub enum Kind {
Transport,
}
impl Error {
pub fn new(kind: Kind) -> Self {
Self {
inner: ErrorImpl { kind, source: None },
}
}
pub fn with(mut self, source: impl Into<Source>) -> Self {
self.inner.source = Some(source.into());
self
}
pub fn from_source(source: impl Into<crate::Error>) -> Self {
Error::new(Kind::Transport).with(source)
}
fn description(&self) -> &str {
match &self.inner.kind {
Kind::Transport => "transport error",
}
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut f = f.debug_tuple("tonic::transport::Error");
f.field(&self.inner.kind);
if let Some(source) = &self.inner.source {
f.field(source);
}
f.finish()
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.description())
}
}
impl StdError for Error {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self.inner
.source
.as_ref()
.map(|source| &**source as &(dyn StdError + 'static))
}
}