zaun_client/error.rs
1//! What can go wrong. Messages never carry invitation secrets, keys or message content.
2
3use zaun_community_protocol::envelope::ErrorBody;
4
5/// A `zaun-client` failure.
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8 /// The SimpleX client failed or refused.
9 #[error("simplex: {0}")]
10 #[cfg(not(target_arch = "wasm32"))]
11 Simplex(#[from] simplex_client::Error),
12 /// The SimpleX client failed or refused.
13 #[error("simplex: {0}")]
14 #[cfg(target_arch = "wasm32")]
15 Simplex(#[from] crate::sx::SimplexClosed),
16 /// The host refused the command with this protocol error.
17 #[error("the host refused {kind}: {code}")]
18 Refused {
19 /// The command.
20 kind: &'static str,
21 /// The protocol error code.
22 code: String,
23 /// The whole error body.
24 body: Box<ErrorBody>,
25 },
26 /// The text is not a Zaun invitation link.
27 #[error("not a valid Zaun invitation link")]
28 BadLink,
29 /// The community is not one this client belongs to.
30 #[error("unknown community")]
31 UnknownCommunity,
32 /// The host connection is not post-quantum; the client closed it.
33 #[error("the host connection is not post-quantum; refused")]
34 NotPostQuantum,
35 /// The host's welcome failed verification.
36 #[error("the host failed verification: {0}")]
37 HostRejected(String),
38 /// A command could not be built or sent.
39 #[error("{0}")]
40 Protocol(String),
41 /// No answer in time.
42 #[error("timed out waiting for {0}")]
43 Timeout(&'static str),
44 /// The community's host does not support this (its welcome did not list the capability),
45 /// so nothing was sent: a host without `embeds` would drop them silently.
46 #[error("the host does not support {0}")]
47 Unsupported(&'static str),
48 /// A file transfer failed (an attachment upload or download).
49 #[error("attachment: {0}")]
50 Transfer(&'static str),
51 /// A voice session could not start or was refused locally (already in voice, a host
52 /// media policy this client does not accept).
53 #[error("voice: {0}")]
54 Voice(String),
55 /// Audio could not be played: a resource that cannot be opened, ffmpeg missing or
56 /// refused, an option out of range. Names no path or URL.
57 #[error("audio: {0}")]
58 Audio(String),
59 /// The local store could not be read or written.
60 #[error("store: {0}")]
61 Store(String),
62 /// The client has stopped.
63 #[error("the client has stopped")]
64 Closed,
65}
66
67/// `Result` with [`Error`].
68pub type Result<T, E = Error> = std::result::Result<T, E>;
69
70/// Turns a refused or undecodable response into an [`Error`].
71pub(crate) fn refused(kind: &'static str, e: Option<ErrorBody>) -> Error {
72 match e {
73 Some(body) => Error::Refused {
74 kind,
75 code: serde_json::to_value(body.code)
76 .ok()
77 .and_then(|v| v.as_str().map(str::to_owned))
78 .unwrap_or_default(),
79 body: Box::new(body),
80 },
81 None => Error::Protocol(format!("{kind}: the host's answer did not decode")),
82 }
83}