pi_hole_api/
errors.rs

1use crate::api_types::FTLNotRunning;
2use std::io;
3
4#[derive(Debug)]
5pub enum APIError {
6    RequestError(ureq::Error),
7    IntoJsonError(io::Error),
8    SerdeJSONError(serde_json::Error),
9    MissingAPIKey,
10    InvalidList,
11    FTLNotRunning,
12}
13
14impl From<ureq::Error> for APIError {
15    fn from(error: ureq::Error) -> Self {
16        APIError::RequestError(error)
17    }
18}
19
20impl From<io::Error> for APIError {
21    fn from(error: io::Error) -> Self {
22        APIError::IntoJsonError(error)
23    }
24}
25
26impl From<serde_json::Error> for APIError {
27    fn from(error: serde_json::Error) -> Self {
28        APIError::SerdeJSONError(error)
29    }
30}
31
32/// Filter out response errors from the API
33pub fn detect_response_errors(response_text: &str) -> Result<(), APIError> {
34    if response_text.starts_with("Invalid list") {
35        return Err(APIError::InvalidList);
36    }
37    if let Ok(ftl_response) = serde_json::from_str::<FTLNotRunning>(response_text) {
38        if !ftl_response.ftl_not_running {
39            assert!(!ftl_response.ftl_not_running);
40            return Err(APIError::FTLNotRunning);
41        }
42    }
43
44    Ok(())
45}