radion_sdk/error.rs
1//! Error types surfaced by the SDK.
2
3/// Convenience alias for results returned by the SDK.
4pub type Result<T, E = RadionError> = std::result::Result<T, E>;
5
6/// Every error surfaced by the Radion SDK.
7///
8/// Mirrors the TypeScript / Python error hierarchy: connection-lifecycle
9/// misuse, server-reported `error` frames, and transport / parse failures.
10/// `Clone` so errors can be fanned out over the lifecycle event stream.
11#[derive(Debug, Clone, thiserror::Error)]
12#[non_exhaustive]
13pub enum RadionError {
14 /// The SDK was used in a way the connection lifecycle forbids — for example
15 /// subscribing after [`close`](crate::realtime::RealtimeClient::close), or
16 /// building a client without an API key.
17 #[error("{0}")]
18 Connection(String),
19
20 /// The server reported an `error` frame.
21 #[error("{message}")]
22 Server {
23 /// Human-readable error message.
24 message: String,
25 /// Machine-readable error code, when present.
26 code: Option<String>,
27 /// Channel the error relates to, when present.
28 channel: Option<String>,
29 /// Subscription id the error relates to, when present.
30 id: Option<String>,
31 },
32
33 /// The underlying WebSocket transport failed.
34 #[error("transport error: {0}")]
35 Transport(String),
36
37 /// A compressed binary frame could not be inflated.
38 #[error("decompression error: {0}")]
39 Decompression(String),
40}
41
42impl RadionError {
43 /// Construct a [`RadionError::Connection`].
44 pub(crate) fn connection(message: impl Into<String>) -> Self {
45 Self::Connection(message.into())
46 }
47
48 /// Construct a [`RadionError::Transport`] from any displayable source.
49 #[cfg(feature = "realtime")]
50 pub(crate) fn transport(source: impl std::fmt::Display) -> Self {
51 Self::Transport(source.to_string())
52 }
53
54 /// Construct a [`RadionError::Decompression`] from any displayable source.
55 #[cfg(feature = "compression")]
56 pub(crate) fn decompression(source: impl std::fmt::Display) -> Self {
57 Self::Decompression(source.to_string())
58 }
59}