Skip to main content

notifica_rust_sdk/
error.rs

1/// All errors that can be returned by [`crate::NotificaClient`].
2#[derive(Debug, thiserror::Error)]
3pub enum NotificaError {
4    /// The underlying HTTP request failed (network issue, timeout, DNS, etc.).
5    #[error("HTTP request failed: {0}")]
6    Http(#[from] reqwest::Error),
7
8    /// Payload could not be serialized to JSON.
9    #[error("serialization error: {0}")]
10    Serialization(#[from] serde_json::Error),
11
12    /// The Notifica service returned a non-200 status code.
13    #[error("notifica service returned unexpected status {status}: {body}")]
14    UnexpectedResponse { status: u16, body: String },
15
16    /// The client was constructed with an invalid configuration.
17    #[error("client configuration error: {0}")]
18    Configuration(String),
19
20    /// The queue provider failed to initialise or publish.
21    #[cfg(feature = "queue")]
22    #[error("queue error: {0}")]
23    Queue(String),
24}
25
26/// Convenience alias used throughout the crate.
27pub type NotificaResult<T> = Result<T, NotificaError>;
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn unexpected_response_formats_correctly() {
35        let err = NotificaError::UnexpectedResponse {
36            status: 404,
37            body: "not found".into(),
38        };
39        assert_eq!(
40            err.to_string(),
41            "notifica service returned unexpected status 404: not found"
42        );
43    }
44
45    #[test]
46    fn configuration_error_formats_correctly() {
47        let err = NotificaError::Configuration("base_url is empty".into());
48        assert_eq!(
49            err.to_string(),
50            "client configuration error: base_url is empty"
51        );
52    }
53}