teter_common/error.rs
1//! Error types for the teter protocol.
2
3use thiserror::Error;
4
5/// Errors that can occur during message serialization or deserialization.
6#[derive(Debug, Error)]
7pub enum ProtocolError {
8 /// Failed to serialize a message to JSON.
9 #[error("failed to serialize message: {0}")]
10 Serialization(serde_json::Error),
11
12 /// Received text is not a message: not JSON, not a JSON object, or a
13 /// `type` that is not a string.
14 #[error("failed to decode message: {0}")]
15 Deserialization(serde_json::Error),
16
17 /// Message type field is missing or empty.
18 #[error("missing or empty message type")]
19 MissingType,
20
21 /// Unknown message type received.
22 #[error("unknown message type: {0}")]
23 UnknownType(String),
24
25 /// Payload is present but cannot be deserialized for the given type.
26 #[error("failed to deserialize payload for message type '{type_}': {source}")]
27 InvalidPayload {
28 /// The message type whose payload failed.
29 type_: String,
30 /// Why it failed.
31 source: serde_json::Error,
32 },
33
34 /// The `session` field is present but is not a session ID (a UUID string).
35 #[error("invalid session ID in message: {source}")]
36 InvalidSession {
37 /// Why the field is not a session ID.
38 source: serde_json::Error,
39 },
40
41 /// The `channel` field is present but is not a channel ID: 1 to 64 ASCII
42 /// letters or digits.
43 #[error("invalid channel in message")]
44 InvalidChannel,
45
46 /// Session ID is missing from a message that requires one.
47 #[error("missing session ID in message")]
48 MissingSession,
49
50 /// Connection was closed unexpectedly.
51 #[error("connection closed unexpectedly")]
52 ConnectionClosed,
53
54 /// Authentication failed.
55 #[error("authentication failed: {reason}")]
56 AuthFailed {
57 /// Why, as the relay says.
58 reason: String,
59 },
60
61 /// Session not found.
62 #[error("session not found: {session_id}")]
63 SessionNotFound {
64 /// The session asked for.
65 session_id: String,
66 },
67
68 /// Session is full (maximum frontend connections reached).
69 #[error("session is full, maximum frontend connections reached")]
70 SessionFull,
71
72 /// Internal relay error.
73 #[error("internal error: {0}")]
74 Internal(String),
75}
76
77/// Result type alias for protocol operations.
78pub type ProtocolResult<T> = Result<T, ProtocolError>;