Skip to main content

home_assistant_rest/
errors.rs

1use std::fmt;
2
3#[derive(Debug)]
4pub enum Error {
5    UrlParseFailed(url::ParseError),
6    RequestFailed(reqwest::Error),
7    DeserializeFailed(serde_json::error::Error),
8}
9
10impl std::error::Error for Error {}
11
12impl fmt::Display for Error {
13    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
14        match self {
15            Self::UrlParseFailed(e) => {
16                write!(f, "Unable to parse the URL: {}", e)
17            }
18            Self::RequestFailed(e) => {
19                write!(f, "Unable to send the request: {}", e)
20            }
21            Self::DeserializeFailed(e) => {
22                write!(f, "Unable to deserialize the received value: {}", e)
23            }
24        }
25    }
26}
27
28impl From<url::ParseError> for Error {
29    fn from(value: url::ParseError) -> Self {
30        Self::UrlParseFailed(value)
31    }
32}
33
34impl From<serde_json::error::Error> for Error {
35    fn from(error: serde_json::error::Error) -> Self {
36        Self::DeserializeFailed(error)
37    }
38}
39
40impl From<reqwest::Error> for Error {
41    fn from(error: reqwest::Error) -> Self {
42        Self::RequestFailed(error)
43    }
44}