smarthome_sdk_rs/
errors.rs1use std::fmt::Display;
2
3use reqwest::StatusCode;
4
5use crate::SERVER_VERSION_REQUIREMENT;
6
7pub type Result<T> = std::result::Result<T, Error>;
8
9#[derive(Debug)]
10pub enum Error {
11 UrlParse(url::ParseError),
13 Reqwest(reqwest::Error),
15 Smarthome(reqwest::StatusCode),
17 VersionParse(semver::Error),
19 IncompatibleVersion(String),
21}
22
23impl From<reqwest::Error> for Error {
24 fn from(err: reqwest::Error) -> Self {
25 Self::Reqwest(err)
26 }
27}
28
29impl From<url::ParseError> for Error {
30 fn from(err: url::ParseError) -> Self {
31 Self::UrlParse(err)
32 }
33}
34
35impl From<semver::Error> for Error {
36 fn from(err: semver::Error) -> Self {
37 Self::VersionParse(err)
38 }
39}
40
41impl Display for Error {
42 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43 let message = match self {
44 Error::UrlParse(err) =>
45 format!("Could not parse URL: {}", err),
46 Error::Reqwest(err) => format!("Request error: {err}"),
47 Error::Smarthome(status_code) => format!("Smarthome error ({status_code}):\n{}", match *status_code {
48 StatusCode::UNAUTHORIZED => "Login failed: invalid credentials\n => Validate your credentials",
49 StatusCode::FORBIDDEN => "Access to this resource has been denied.\n => You are possibly lacking permission to access the requested resource",
50 StatusCode::SERVICE_UNAVAILABLE => "Smarthome is currently unavailable\n => The server has significant issues and was unable to respond properly",
51 StatusCode::CONFLICT => "The requested action conflicts with other data on the system\n => Identify those conflicts and repeat the current action",
52 _ => "Unimplemented status code: please open an issue on Github here: (https://github.com/smarthome-go/sdk-rs)"
53 }),
54 Error::VersionParse(err) => panic!("Internal error: a version is invalid and could not be parsed: this is a bug and not your fault: {err}"),
55 Error::IncompatibleVersion(server_version) => format!("Incompatible server version: the server version is `{server_version}` but this program requires `{}`", SERVER_VERSION_REQUIREMENT)
56 };
57 write!(f, "{message}")
58 }
59}