Skip to main content

offline_protocol_core/
error.rs

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