1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
use futures_core::stream::TryStream;
use futures_sink::Sink;
use std::{error, fmt};

/// An error that occurred while servicing a request.
pub enum Error<T, I>
where
    T: Sink<I> + TryStream,
{
    /// The underlying transport failed to send a request.
    BrokenTransportSend(<T as Sink<I>>::Error),

    /// The underlying transport failed while attempting to receive a response.
    ///
    /// If `None`, the transport closed without error while there were pending requests.
    BrokenTransportRecv(Option<<T as TryStream>::Error>),

    /// Attempted to issue a `call` when no more requests can be in flight.
    ///
    /// See [`tower_service::Service::poll_ready`] and [`Client::with_limit`].
    TransportFull,

    /// Attempted to issue a `call`, but the underlying transport has been closed.
    ClientDropped,

    /// The server sent a response that the client was not expecting.
    Desynchronized,
}

impl<T, I> fmt::Display for Error<T, I>
where
    T: Sink<I> + TryStream,
    <T as Sink<I>>::Error: fmt::Display,
    <T as TryStream>::Error: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Error::BrokenTransportSend(ref se) => fmt::Display::fmt(se, f),
            Error::BrokenTransportRecv(Some(ref se)) => fmt::Display::fmt(se, f),
            Error::BrokenTransportRecv(None) => f.pad("transport closed with in-flight requests"),
            Error::TransportFull => f.pad("no more in-flight requests allowed"),
            Error::ClientDropped => f.pad("Client was dropped"),
            Error::Desynchronized => f.pad("server sent a response the client did not expect"),
        }
    }
}

impl<T, I> fmt::Debug for Error<T, I>
where
    T: Sink<I> + TryStream,
    <T as Sink<I>>::Error: fmt::Debug,
    <T as TryStream>::Error: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            Error::BrokenTransportSend(ref se) => write!(f, "BrokenTransportSend({:?})", se),
            Error::BrokenTransportRecv(Some(ref se)) => write!(f, "BrokenTransportRecv({:?})", se),
            Error::BrokenTransportRecv(None) => f.pad("BrokenTransportRecv"),
            Error::TransportFull => f.pad("TransportFull"),
            Error::ClientDropped => f.pad("ClientDropped"),
            Error::Desynchronized => f.pad("Desynchronized"),
        }
    }
}

impl<T, I> error::Error for Error<T, I>
where
    T: Sink<I> + TryStream,
    <T as Sink<I>>::Error: error::Error,
    <T as TryStream>::Error: error::Error,
{
    fn cause(&self) -> Option<&dyn error::Error> {
        match *self {
            Error::BrokenTransportSend(ref se) => Some(se),
            Error::BrokenTransportRecv(Some(ref se)) => Some(se),
            _ => None,
        }
    }

    #[allow(deprecated)]
    fn description(&self) -> &str {
        match *self {
            Error::BrokenTransportSend(ref se) => se.description(),
            Error::BrokenTransportRecv(Some(ref se)) => se.description(),
            Error::BrokenTransportRecv(None) => "transport closed with in-flight requests",
            Error::TransportFull => "no more in-flight requests allowed",
            Error::ClientDropped => "Client was dropped",
            Error::Desynchronized => "server sent a response the client did not expect",
        }
    }
}

impl<T, I> Error<T, I>
where
    T: Sink<I> + TryStream,
{
    pub(crate) fn from_sink_error(e: <T as Sink<I>>::Error) -> Self {
        Error::BrokenTransportSend(e)
    }

    pub(crate) fn from_stream_error(e: <T as TryStream>::Error) -> Self {
        Error::BrokenTransportRecv(Some(e))
    }
}