Skip to main content

tuwunel_service/fetcher/
error.rs

1//! Internal failure type for a fetch.
2
3use std::fmt;
4
5use ruma::OwnedServerName;
6use tuwunel_core::{err, smallvec::SmallVec};
7
8/// Servers tried before a fetch gave up; sized to the candidate-pool budget.
9pub(super) type Attempted = SmallVec<[OwnedServerName; 3]>;
10
11/// Internal failure shape. Kept `Clone` so it can ride the shared-result
12/// channel to every coalesced caller; converted to [`tuwunel_core::Error`] at
13/// the public boundary. Carries the servers tried for the operator-facing
14/// message.
15#[derive(Clone, Debug)]
16pub(super) enum Failure {
17	/// Every candidate was tried and none returned a valid response.
18	NotFound {
19		attempted: Attempted,
20	},
21
22	/// No candidate servers were available to try.
23	NoCandidates,
24
25	/// All callers dropped the future before a server answered.
26	Cancelled,
27}
28
29impl fmt::Display for Failure {
30	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31		match self {
32			| Self::NoCandidates => write!(f, "no candidate servers available"),
33			| Self::Cancelled => write!(f, "fetch cancelled"),
34			| Self::NotFound { attempted } => {
35				write!(f, "event not found on any of {} servers", attempted.len())
36			},
37		}
38	}
39}
40
41impl From<Failure> for tuwunel_core::Error {
42	fn from(failure: Failure) -> Self { err!(Request(NotFound("{failure}"))) }
43}