Skip to main content

zaun_community_protocol/envelope/
error_extras.rs

1//! Constructors for the error codes that carry extra fields (§11).
2
3use std::borrow::Cow;
4
5use super::{ErrorBody, RateCategory, RateScope, Reason, Stream, VersionRange};
6use crate::error_code::ErrorCode;
7use crate::ids::ChannelId;
8
9impl ErrorBody {
10    /// `BAD_REQUEST`, naming the offending field path when known (`"body.content"`). The
11    /// path names a schema field; never put a received value in it.
12    #[must_use]
13    pub fn bad_request(field: Option<&str>) -> Self {
14        let mut e = Self::new(ErrorCode::BadRequest, "malformed request");
15        e.extras.field = field.map(str::to_owned);
16        e
17    }
18
19    /// `PAYLOAD_TOO_LARGE` with the limit exceeded, in bytes or items.
20    #[must_use]
21    pub fn payload_too_large(limit: u64) -> Self {
22        let mut e = Self::new(ErrorCode::PayloadTooLarge, "payload too large");
23        e.extras.limit = Some(limit);
24        e
25    }
26
27    /// `UNSUPPORTED_VERSION` with the host's supported range.
28    #[must_use]
29    pub fn unsupported_version(supported: VersionRange) -> Self {
30        let mut e = Self::new(
31            ErrorCode::UnsupportedVersion,
32            "unsupported protocol version",
33        );
34        e.extras.supported = Some(supported);
35        e
36    }
37
38    /// `UNSUPPORTED_FEATURE` naming the feature (`"attachments"`, `"voice"`, a reserved
39    /// command name). The name may be dynamic (`BodyError::UnsupportedFeature`) but must be a
40    /// protocol name, never text taken from a secret or another member (§11).
41    #[must_use]
42    pub fn unsupported_feature(feature: impl Into<Cow<'static, str>>) -> Self {
43        let mut e = Self::new(ErrorCode::UnsupportedFeature, "feature not supported");
44        e.extras.feature = Some(feature.into().into_owned());
45        e
46    }
47
48    /// `FORBIDDEN`, with the missing permission bits and the cause when known.
49    #[must_use]
50    pub fn forbidden(missing: Option<u64>, reason: Option<Reason>) -> Self {
51        let mut e = Self::new(ErrorCode::Forbidden, "forbidden");
52        e.extras.missing = missing;
53        e.extras.reason = reason;
54        e
55    }
56
57    /// `SUSPENDED`, with the end of the suspension when it has one.
58    #[must_use]
59    pub fn suspended(until: Option<u64>) -> Self {
60        let mut e = Self::new(ErrorCode::Suspended, "membership is suspended");
61        e.extras.until = until;
62        e
63    }
64
65    /// `CONFLICT` with its reason.
66    #[must_use]
67    pub fn conflict(reason: Reason) -> Self {
68        let mut e = Self::new(ErrorCode::Conflict, "conflict");
69        e.extras.reason = Some(reason);
70        e
71    }
72
73    /// `INVALID_SIGNATURE` naming the signed object (`"descriptor"`, `"delegation"`, …).
74    #[must_use]
75    pub fn invalid_signature(object: &'static str) -> Self {
76        let mut e = Self::new(ErrorCode::InvalidSignature, "invalid signature");
77        e.extras.object = Some(object.to_owned());
78        e
79    }
80
81    /// `SLOWMODE_ACTIVE` (message as in the §7.2 example).
82    #[must_use]
83    pub fn slowmode_active(retry_after_ms: u64) -> Self {
84        let mut e = Self::new(ErrorCode::SlowmodeActive, "slowmode is active");
85        e.extras.retry_after_ms = Some(retry_after_ms);
86        e
87    }
88
89    /// `RATE_LIMITED` with the scope and, when one applies, the category.
90    #[must_use]
91    pub fn rate_limited(
92        retry_after_ms: u64,
93        scope: RateScope,
94        category: Option<RateCategory>,
95    ) -> Self {
96        let mut e = Self::new(ErrorCode::RateLimited, "rate limited");
97        e.extras.retry_after_ms = Some(retry_after_ms);
98        e.extras.scope = Some(scope);
99        e.extras.category = category;
100        e
101    }
102
103    /// `TIMED_OUT` until the given time.
104    #[must_use]
105    pub fn timed_out(until: u64) -> Self {
106        let mut e = Self::new(ErrorCode::TimedOut, "member is timed out");
107        e.extras.until = Some(until);
108        e
109    }
110
111    /// `BUSY`: the host's bounded queues are full.
112    #[must_use]
113    pub fn busy(retry_after_ms: u64) -> Self {
114        let mut e = Self::new(ErrorCode::Busy, "host is busy");
115        e.extras.retry_after_ms = Some(retry_after_ms);
116        e
117    }
118
119    /// `RESYNC_REQUIRED` for the member stream.
120    #[must_use]
121    pub fn resync_required_member() -> Self {
122        let mut e = Self::new(ErrorCode::ResyncRequired, "resynchronisation required");
123        e.extras.stream = Some(Stream::Member);
124        e
125    }
126
127    /// `RESYNC_REQUIRED` for a channel stream.
128    #[must_use]
129    pub fn resync_required_channel(channel_id: ChannelId) -> Self {
130        let mut e = Self::resync_required_member();
131        e.extras.stream = Some(Stream::Channel);
132        e.extras.channel_id = Some(channel_id);
133        e
134    }
135}