hass_rs/
errors.rs

1//! Convenient error handling
2
3use crate::types::Response;
4use crate::types::WSResult;
5use thiserror::Error;
6use tokio_tungstenite::tungstenite;
7
8pub type HassResult<T> = std::result::Result<T, HassError>;
9
10/// The error enum for Hass
11#[derive(Error, Debug)]
12pub enum HassError {
13    /// Returned when it is unable to authenticate
14    #[error("Authentication failed: {0}")]
15    AuthenticationFailed(String),
16
17    /// Returned when serde was unable to deserialize the values
18    #[error("Unable to deserialize received value: {0}")]
19    UnableToDeserialize(#[from] serde_json::error::Error),
20
21    /// Returned when connection has unexpected failed
22    #[error("Connection closed unexpectedly")]
23    ConnectionClosed,
24
25    /// Mpsc channel SendError<T> message
26    #[error("Unable to send the message on channel: {0}")]
27    SendError(String),
28
29    #[error("Unable to receive a message on channel: {0}")]
30    RecvError(String),
31
32    /// Tungstenite error
33    #[error("Tungstenite error: {0}")]
34    TungsteniteError(#[from] tungstenite::error::Error),
35
36    /// Returned when an unknown message format is received
37    #[error("The received payload is unknown {0:?}")]
38    UnknownPayloadReceived(Response),
39
40    /// Returned when an unknown message format is received
41    #[error("Received an unexpected message: {0:?}")]
42    UnexpectedMessage(tungstenite::Message),
43
44    /// Returned the error received from the Home Assistant Gateway
45    #[error("ResponseError: {0:?}")]
46    ResponseError(WSResult),
47
48    /// Returned for errors which do not fit any of the above criterias
49    #[error("Generic Error: {0}")]
50    Generic(String),
51}