Skip to main content

zaun_community_protocol/envelope/
errors.rs

1//! One constructor per error code (§11), so the engine never builds error JSON by hand. Codes
2//! with extra fields are in `error_extras.rs`.
3//!
4//! Messages are fixed English strings. Extras are typed values or protocol labels (field and
5//! feature names), so nothing taken from a secret, a connection or another member can reach
6//! the wire (§11).
7
8use super::ErrorBody;
9use crate::error_code::ErrorCode;
10
11macro_rules! no_extras {
12    ($($(#[$doc:meta])* $name:ident => $code:ident, $message:literal;)*) => {
13        $(
14            $(#[$doc])*
15            #[must_use]
16            pub fn $name() -> Self {
17                Self::new(ErrorCode::$code, $message)
18            }
19        )*
20    };
21}
22
23impl ErrorBody {
24    /// A body with only `code` and `message`.
25    #[must_use]
26    pub fn new(code: ErrorCode, message: &'static str) -> Self {
27        Self {
28            code,
29            message: message.to_owned(),
30            extras: Box::default(),
31        }
32    }
33
34    /// Replaces the default message with another fixed one.
35    #[must_use]
36    pub fn with_message(mut self, message: &'static str) -> Self {
37        message.clone_into(&mut self.message);
38        self
39    }
40
41    /// Whether a response carrying this error is stored as the idempotency record (§7.3).
42    #[must_use]
43    pub const fn is_recorded(&self) -> bool {
44        self.code.is_recorded()
45    }
46
47    no_extras! {
48        /// `UNSUPPORTED_COMMAND`: unknown `type`.
49        unsupported_command => UnsupportedCommand, "unsupported command";
50        /// `HELLO_REQUIRED`: a command before a successful `session.hello`.
51        hello_required => HelloRequired, "session.hello required";
52        /// `NOT_BOUND`: a bound-only command on an unbound connection.
53        not_bound => NotBound, "connection is not bound";
54        /// `WRONG_COMMUNITY`: `community_id` differs from the binding.
55        wrong_community => WrongCommunity, "wrong community";
56        /// `HOST_NOT_AUTHORIZED`: no valid delegation for the community.
57        host_not_authorized => HostNotAuthorized, "host is not authorized for this community";
58        /// `PENDING_APPROVAL`: the member is pending.
59        pending_approval => PendingApproval, "membership is pending approval";
60        /// `ADMISSION_CLOSED` (one code for every cause, so it is not an oracle).
61        admission_closed => AdmissionClosed, "admission is closed";
62        /// `NOT_FOUND` (missing and invisible are indistinguishable).
63        not_found => NotFound, "not found";
64        /// `INVITE_INVALID` (one code for every cause, so it is not an oracle).
65        invite_invalid => InviteInvalid, "invitation is invalid";
66        /// `BANNED`: sent once, then the session ends.
67        banned => Banned, "member is banned";
68        /// `INTERNAL`: details are logged on the host, never sent.
69        internal => Internal, "internal error";
70    }
71}