Skip to main content

web_scrape/
error.rs

1use crate::scrape;
2use crate::scrape::ScrapeError;
3use clerr::Report;
4use reqwest::StatusCode;
5use std::fmt::{Display, Formatter};
6use std::string::FromUtf8Error;
7use web_url::WebUrl;
8
9/// An error scraping data from the web.
10#[derive(Debug)]
11pub enum Error {
12    /// An uncategorized error.
13    Other(Report),
14
15    /// The web data was not properly UTF-8 encoded.
16    InvalidString(FromUtf8Error),
17
18    /// The response status code was invalid.
19    InvalidResponseStatus(StatusCode),
20
21    /// The URL was invalid.
22    InvalidURL { url: WebUrl, error_message: String },
23
24    /// There was an error reading or writing the cache.
25    Cache(file_storage::Error),
26
27    /// There was an error sourcing the data.
28    Source(reqwest::Error),
29
30    /// There was an error scraping the data.
31    Scrape(scrape::ScrapeError),
32}
33
34impl From<Report> for Error {
35    fn from(report: Report) -> Self {
36        Self::Other(report)
37    }
38}
39
40impl From<FromUtf8Error> for Error {
41    fn from(error: FromUtf8Error) -> Self {
42        Self::InvalidString(error)
43    }
44}
45
46impl From<file_storage::Error> for Error {
47    fn from(error: file_storage::Error) -> Self {
48        Self::Cache(error)
49    }
50}
51
52impl From<reqwest::Error> for Error {
53    fn from(error: reqwest::Error) -> Self {
54        Self::Source(error)
55    }
56}
57
58impl From<ScrapeError> for Error {
59    fn from(error: ScrapeError) -> Self {
60        Self::Scrape(error)
61    }
62}
63
64impl Display for Error {
65    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
66        write!(f, "{:?}", self)
67    }
68}
69
70impl std::error::Error for Error {}