Skip to main content

web_scrape/
error.rs

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