Skip to main content

zaun_community_protocol/client/
error.rs

1//! Client errors. None of them carries body text, so no secret can reach a log through them.
2
3use crate::command::BodyError;
4use crate::envelope::VersionRange;
5use crate::framing::EncodeError;
6use crate::ids::RngError;
7use crate::signed::{SessionProofError, SignedError, ValidityError};
8
9/// A command could not be prepared or the session refuses to send it.
10#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
11pub enum ClientError {
12    /// The body breaks a state-free rule (§12); nothing was sent.
13    #[error("invalid body: {0}")]
14    InvalidBody(BodyError),
15    /// The body or envelope failed to serialise (never expected for this crate's types).
16    #[error("serialisation failed")]
17    Serialize,
18    /// The envelope exceeds its size limit (§12) or cannot be framed (§5).
19    #[error(transparent)]
20    Frame(#[from] EncodeError),
21    /// The envelope exceeds the command limit for its type (§12).
22    #[error("envelope of {len} bytes exceeds the {limit}-byte limit")]
23    TooLarge {
24        /// Envelope length.
25        len: usize,
26        /// The limit for its type.
27        limit: usize,
28    },
29    /// The OS random number generator failed.
30    #[error(transparent)]
31    Rng(#[from] RngError),
32    /// No verified `session.welcome` yet: call [`super::Session::hello`] and wait (§6.2).
33    #[error("the session has no verified welcome")]
34    NotReady,
35    /// The welcome failed verification; the session MUST NOT send commands (§6.2).
36    #[error("the session failed verification: {0}")]
37    Failed(WelcomeError),
38    /// `community_id` differs from the session's binding (§7.1, `WRONG_COMMUNITY`).
39    #[error("community differs from the session's binding")]
40    WrongCommunity,
41    /// [`super::Session::send_signal`] with a non-signal body, or `send` with a signal.
42    #[error("{0} cannot be sent this way")]
43    WrongKind(&'static str),
44}
45
46/// Why a `session.welcome` was rejected (§4.3, §4.5, §6.2).
47#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
48pub enum WelcomeError {
49    /// The welcome body is not a valid `session.welcome`.
50    #[error("malformed welcome")]
51    Malformed,
52    /// The negotiated version is outside the range this client offered.
53    #[error("negotiated version {version} outside {offered:?}")]
54    Version {
55        /// The host's choice.
56        version: u16,
57        /// The client's range.
58        offered: VersionRange,
59    },
60    /// The host key or id is not the expected host.
61    #[error("unexpected host")]
62    Host,
63    /// The welcome's `host_id` does not derive from its `host_public_key`.
64    #[error("host id does not derive from the host key")]
65    HostId,
66    /// A community was expected but the welcome carries no descriptor, delegation or proof
67    /// naming it.
68    #[error("the host did not prove the expected community")]
69    MissingCommunity,
70    /// The welcome binds the connection to another community.
71    #[error("binding names another community")]
72    Binding,
73    /// The descriptor/delegation pair is not valid (§4.3).
74    #[error(transparent)]
75    Pair(#[from] ValidityError),
76    /// The delegation does not name this host or lacks `OPERATE` (§4.2).
77    #[error("the delegation does not authorise this host")]
78    Delegation,
79    /// The rotation chain is longer than the client knows; fetch `community.rotations`.
80    #[error("the rotation chain is incomplete")]
81    ChainIncomplete,
82    /// The host session proof failed (§4.5).
83    #[error(transparent)]
84    Proof(#[from] SessionProofError),
85    /// The proof's challenge differs from the welcome's.
86    #[error("challenge mismatch")]
87    Challenge,
88    /// The host answered `session.hello` with an error (for example `UNSUPPORTED_VERSION`).
89    #[error("the host refused the hello")]
90    Refused,
91}
92
93/// An owner or member object (descriptor, delegation, registration, body) could not be built.
94#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
95pub enum BuildError {
96    /// The advertised host key does not derive the advertised `HostId`.
97    #[error("host key does not derive the host id")]
98    HostId,
99    /// The signing key is not the descriptor's current root key.
100    #[error("the key is not the current root key")]
101    WrongRootKey,
102    /// The member key is not the descriptor's `owner_member_key`.
103    #[error("the member key is not the owner binding")]
104    OwnerKey,
105    /// A field breaks its rule, or signing failed.
106    #[error(transparent)]
107    Signed(#[from] SignedError),
108    /// The command body breaks a state-free rule (§12).
109    #[error("invalid body: {0}")]
110    Body(BodyError),
111}