Skip to main content

hopr_transport_session/
errors.rs

1use hopr_api::types::crypto::prelude::SimplePseudonym;
2use thiserror::Error;
3
4/// Enumeration of errors thrown from this library.
5#[derive(Error, Debug)]
6pub enum TransportSessionError {
7    #[error("session operation timed out")]
8    Timeout,
9
10    #[error("unparseable session id")]
11    InvalidSessionId,
12
13    #[error("the other party rejected session initiation with error: {0}")]
14    Rejected(hopr_protocol_start::StartErrorReason),
15
16    #[error("received data for an unregistered session")]
17    UnknownData,
18
19    #[error("packet sending error: {0}")]
20    PacketSendingError(anyhow::Error),
21
22    #[error(transparent)]
23    StartProtocolError(#[from] hopr_protocol_start::errors::StartProtocolError),
24
25    #[error(transparent)]
26    SessionProtocolError(#[from] hopr_protocol_session::errors::SessionError),
27
28    #[error(transparent)]
29    Manager(#[from] SessionManagerError),
30
31    #[error(transparent)]
32    Network(#[from] hopr_utils::network_types::errors::NetworkTypeError),
33
34    #[error("session is closed")]
35    Closed,
36
37    #[error("invalid configuration: {0}")]
38    InvalidConfig(String),
39}
40
41impl TransportSessionError {
42    pub fn packet_sending<E: Into<anyhow::Error>>(e: E) -> Self {
43        Self::PacketSendingError(e.into())
44    }
45}
46
47impl From<TransportSessionError> for std::io::Error {
48    fn from(error: TransportSessionError) -> Self {
49        std::io::Error::other(error)
50    }
51}
52
53#[derive(Error, Debug)]
54pub enum SessionManagerError {
55    #[error("manager is not started")]
56    NotStarted,
57    #[error("manager is already started")]
58    AlreadyStarted,
59    #[error("all challenge slots are occupied")]
60    NoChallengeSlots,
61    #[error("session with the given id does not exist")]
62    NonExistingSession,
63    #[error("number of sessions exceeds the maximum allowed")]
64    TooManySessions,
65    #[error("loopback sessions are not allowed")]
66    Loopback,
67    #[error("received a Start protocol message that is not supported")]
68    UnsupportedMessage,
69    #[error("one of the parties attempted to negotiate unacceptable Session parameters: {0}")]
70    Unacceptable(String),
71    /// A Start protocol request was dropped without being served, and nothing about the Session was
72    /// found wrong.
73    ///
74    /// The counterpart of [`Unacceptable`](Self::Unacceptable) for refusals that are *not* terminal. It
75    /// is returned for exactly five causes: the request lost a race for the per-Session request lock;
76    /// it came too early, before emission reached the successor boundary; it is under-served, since
77    /// the returned packets have not yet earned it; it is stale, naming indices at or below the
78    /// committed watermark that are not the batch last committed; or the transport cut short the
79    /// sending of a commitment — the Entry's own publication, the resumption of one a repeat or a
80    /// scoped re-request prompted, or the re-send of a published batch's bursts that a late repeat
81    /// prompted. The Session keeps running and the peer is told nothing.
82    ///
83    /// Whether a later request succeeds depends on the cause. The first three can succeed on a later
84    /// re-send of the same request, once the lock is free or the Entry's state has caught up, and the
85    /// Exit does re-send a successor while it is unanswered and its batch is behind the front. A stale
86    /// request is never admitted.
87    ///
88    /// A cut-short publication is finished only if the Exit asks again, and it has two ways to. Its
89    /// automatic repeat of the request is sent only while some member of the batch has reported nothing
90    /// *and* the batch is behind the front. Its scoped re-request for a member whose commitment began to
91    /// arrive works wherever the batch stands, and resumes every owed member of the batch, not only the
92    /// one named, but only when the member it names is itself still owed, because its own burst was cut
93    /// part-way. Two cases are therefore not recovered. A member of which no message went out, whose
94    /// batch holds the front (the opening batch always does), is resumed only as a side effect of a
95    /// scoped re-request for an earlier member of the same batch that is itself still owed. And neither
96    /// path is prompted by a member whose whole burst went out and only its `ReadyToDeposit` could not
97    /// be queued, since the Exit already holds its complete commitment; it stays owed, and is finished
98    /// only incidentally, if a request for another member resumes everything owed. In those cases the
99    /// Exit's own deadline ends the cycle, and nothing is stranded for that member because nothing was
100    /// announced for it.
101    ///
102    /// A repeat of the batch last committed otherwise returns `Ok(())`. If the batch was published in
103    /// full, that is a no-op while the batch went out less than three seconds ago (the manager's
104    /// `SSA_REPEAT_RESEND_GRACE`). A repeat arriving later says the bursts were lost on the way, and
105    /// returns after re-sending those of the announced members. If the batch was not published in
106    /// full, it returns after finishing the publication. Callers that report errors should treat this
107    /// as an ordinary outcome rather than a fault.
108    #[error("the request was not admitted: {0}")]
109    NotAdmitted(String),
110    #[error("deposit pool did not supply deposit data for the SSA batch: {0}")]
111    MissingDepositData(String),
112    #[error(transparent)]
113    PixError(#[from] hopr_protocol_pix::errors::PixError<SimplePseudonym>),
114    #[error(transparent)]
115    Other(anyhow::Error),
116}
117
118impl SessionManagerError {
119    pub fn other<E: Into<anyhow::Error>>(e: E) -> Self {
120        Self::Other(e.into())
121    }
122}
123
124pub type Result<T> = std::result::Result<T, TransportSessionError>;