web_scrape/
error.rs

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