Skip to main content

offline_protocol_core/
error.rs

1//! Error types for the Offline Protocol SDK.
2
3use thiserror::Error;
4
5/// Result type alias using the core Error type.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Core errors that can occur in the Offline Protocol SDK.
9// Adding a variant to a public error enum is a breaking change without
10// this attribute; downstream crates must carry a wildcard arm.
11#[non_exhaustive]
12#[derive(Debug, Error, Clone, PartialEq, Eq)]
13pub enum Error {
14    /// Invalid message format or content.
15    #[error("Invalid message: {0}")]
16    InvalidMessage(String),
17
18    /// Invalid user ID format.
19    #[error("Invalid user ID: {0}")]
20    InvalidUserId(String),
21
22    /// Invalid app ID format.
23    #[error("Invalid app ID: {0}")]
24    InvalidAppId(String),
25
26    /// Invalid TTL value (must be > 0).
27    #[error("Invalid TTL: {0}")]
28    InvalidTTL(u8),
29
30    /// Invalid hop count value.
31    #[error("Invalid hop count: {0}")]
32    InvalidHopCount(u8),
33
34    /// Message serialization failed.
35    #[error("Serialization error: {0}")]
36    SerializationError(String),
37
38    /// Message deserialization failed.
39    #[error("Deserialization error: {0}")]
40    DeserializationError(String),
41
42    /// Invalid service ID format.
43    #[error("Invalid service ID: {0}")]
44    InvalidServiceId(String),
45
46    /// Generic error with custom message.
47    #[error("{0}")]
48    Other(String),
49}
50
51impl From<serde_json::Error> for Error {
52    fn from(err: serde_json::Error) -> Self {
53        Error::SerializationError(err.to_string())
54    }
55}