Skip to main content

ping_core/
error.rs

1use thiserror::Error;
2
3pub type Result<T> = std::result::Result<T, Error>;
4
5#[derive(Debug, Error)]
6pub enum Error {
7    #[error("storage error: {0}")]
8    Storage(String),
9
10    #[error("transport error: {0}")]
11    Transport(String),
12
13    #[error("mls protocol error: {0}")]
14    Mls(String),
15
16    #[error("identity error: {0}")]
17    Identity(String),
18
19    #[error("unknown conversation: {0}")]
20    UnknownConversation(String),
21
22    #[error("unknown device: {0}")]
23    UnknownDevice(String),
24
25    #[error("epoch occupied: caller must rebase on the new epoch and retry")]
26    EpochOccupied,
27
28    /// The KeyPackage (or its private init key) needed to join is gone — it was
29    /// already consumed (single-use reuse) or the recipient's pool is exhausted.
30    /// Host action: top up KeyPackages (publish a last-resort KP) and have the
31    /// inviter retry. Distinct from a generic `Mls` error so the host can react
32    /// instead of treating every join failure the same.
33    #[error("key package not found: already consumed or pool exhausted")]
34    KeyPackageNotFound,
35
36    /// This conversation is behind the group's current epoch and can no longer
37    /// decrypt newer traffic from local state alone — a Commit was missed (the
38    /// device was offline across a rekey, or a Commit was dropped). Host action:
39    /// recover via re-Welcome or a same-user state snapshot. Carries the
40    /// conversation id (hex) so the host knows which chat to repair.
41    #[error("conversation stranded behind epoch (missed commit), needs recovery: {0}")]
42    EpochStranded(String),
43
44    #[error("codec error: {0}")]
45    Codec(String),
46
47    #[error("invalid input: {0}")]
48    Invalid(String),
49
50    #[error("not initialised")]
51    NotInitialised,
52}
53
54impl Error {
55    pub(crate) fn mls<E: std::fmt::Display>(e: E) -> Self {
56        Error::Mls(e.to_string())
57    }
58    pub(crate) fn codec<E: std::fmt::Display>(e: E) -> Self {
59        Error::Codec(e.to_string())
60    }
61}