1use {
2 isahc::http::uri::InvalidUri,
3 std::{error, fmt, io, str::Utf8Error},
4};
5
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8 #[error("html scraping error: {err}")]
9 Scrape {
10 #[from]
11 err: ScrapeError,
12 },
13
14 #[error("http request error: {err}")]
15 Request {
16 #[from]
17 err: RequestError,
18 },
19
20 #[error("invalid url passed: {err}")]
21 Uri {
22 #[from]
23 err: InvalidUri,
24 },
25
26 #[error("tokio thread pool is empty or missing")]
27 Canceled,
28}
29
30#[derive(Debug)]
31pub enum ScrapeError {
32 Io(io::Error),
33 Utf8(Utf8Error),
34
35 ElementNotFound(&'static str, &'static str),
36 AttributeNotFound(&'static str, &'static str),
37 UnparsableDate(&'static str),
38}
39
40impl fmt::Display for ScrapeError {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
42 match self {
43 ScrapeError::Io(ref err) => err.fmt(f),
44 ScrapeError::Utf8(ref err) => err.fmt(f),
45 ScrapeError::ElementNotFound(ref site, ref selector) => write!(
46 f,
47 "Sector element for site {} not found: {}",
48 site, selector
49 ),
50 ScrapeError::AttributeNotFound(ref site, ref selector) => write!(
51 f,
52 "Element attribute for site {} not found: {}",
53 site, selector
54 ),
55 ScrapeError::UnparsableDate(ref site) => {
56 write!(f, "Unparsable date time for site {}", site,)
57 }
58 }
59 }
60}
61
62impl error::Error for ScrapeError {
63 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
64 match self {
65 ScrapeError::Io(ref err) => Some(err),
66 ScrapeError::Utf8(ref err) => Some(err),
67 ScrapeError::ElementNotFound(_, _)
68 | ScrapeError::AttributeNotFound(_, _)
69 | ScrapeError::UnparsableDate(_) => None,
70 }
71 }
72}
73
74impl From<io::Error> for ScrapeError {
75 fn from(err: io::Error) -> ScrapeError {
76 ScrapeError::Io(err)
77 }
78}
79
80impl From<Utf8Error> for ScrapeError {
81 fn from(err: Utf8Error) -> ScrapeError {
82 ScrapeError::Utf8(err)
83 }
84}
85
86#[derive(Debug)]
87pub enum RequestError {
88 Http(isahc::http::Error),
89 Io(io::Error),
90 Isahc(isahc::Error),
91}
92
93impl fmt::Display for RequestError {
94 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
95 match self {
96 RequestError::Http(ref err) => err.fmt(f),
97 RequestError::Io(ref err) => err.fmt(f),
98 RequestError::Isahc(ref err) => err.fmt(f),
99 }
100 }
101}
102
103impl error::Error for RequestError {
104 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
105 match self {
106 RequestError::Http(ref err) => Some(err),
107 RequestError::Io(ref err) => Some(err),
108 RequestError::Isahc(ref err) => Some(err),
109 }
110 }
111}
112
113impl From<isahc::http::Error> for RequestError {
114 fn from(err: isahc::http::Error) -> RequestError {
115 RequestError::Http(err)
116 }
117}
118
119impl From<io::Error> for RequestError {
120 fn from(err: io::Error) -> RequestError {
121 RequestError::Io(err)
122 }
123}
124
125impl From<isahc::Error> for RequestError {
126 fn from(err: isahc::Error) -> RequestError {
127 RequestError::Isahc(err)
128 }
129}