2023-05-31 12:54:51 -04:00
|
|
|
//! This module provides the error enum to handle different errors associated while requesting data from
|
|
|
|
//! the upstream search engines with the search query provided by the user.
|
|
|
|
|
|
|
|
use reqwest::header::InvalidHeaderValue;
|
|
|
|
use scraper::error::SelectorErrorKind;
|
|
|
|
|
|
|
|
/// A custom error type used for handle engine associated errors.
|
|
|
|
///
|
|
|
|
/// This enum provides variants three different categories of errors:
|
|
|
|
/// * `RequestError` - This variant handles all request related errors like forbidden, not found,
|
|
|
|
/// etc.
|
|
|
|
/// * `EmptyResultSet` - This variant handles the not results found error provide by the upstream
|
|
|
|
/// search engines.
|
|
|
|
/// * `UnexpectedError` - This variant handles all the errors which are unexpected or occur rarely
|
|
|
|
/// and are errors mostly related to failure in initialization of HeaderMap, Selector errors and
|
2023-06-04 04:56:07 -04:00
|
|
|
/// all other errors occuring within the code handling the `upstream search engines`.
|
2023-05-16 05:22:00 -04:00
|
|
|
#[derive(Debug)]
|
2023-05-31 12:54:51 -04:00
|
|
|
pub enum EngineErrorKind {
|
|
|
|
RequestError(reqwest::Error),
|
|
|
|
EmptyResultSet,
|
2023-06-04 04:56:07 -04:00
|
|
|
UnexpectedError {
|
|
|
|
message: String,
|
|
|
|
source: Option<Box<dyn std::error::Error>>,
|
|
|
|
},
|
2023-05-31 12:54:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Implementing `Display` trait to make errors writable on the stdout and also providing/passing the
|
|
|
|
/// appropriate errors that should be written to the stdout when this error is raised/encountered.
|
|
|
|
impl std::fmt::Display for EngineErrorKind {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2023-06-04 04:56:07 -04:00
|
|
|
EngineErrorKind::RequestError(request_error) => {
|
|
|
|
write!(f, "Request error: {}", request_error)
|
|
|
|
}
|
2023-05-31 12:54:51 -04:00
|
|
|
EngineErrorKind::EmptyResultSet => {
|
|
|
|
write!(f, "The upstream search engine returned an empty result set")
|
|
|
|
}
|
2023-06-04 04:56:07 -04:00
|
|
|
EngineErrorKind::UnexpectedError { message, source } => {
|
|
|
|
write!(f, "Unexpected error: {}", message)?;
|
|
|
|
if let Some(source) = source {
|
|
|
|
write!(f, "\nCaused by: {}", source)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-05-31 12:54:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-04 04:56:07 -04:00
|
|
|
/// Implementing `Error` trait to make the the `EngineErrorKind` enum an error type and
|
|
|
|
/// mapping `ReqwestErrors` to `RequestError` and `UnexpectedError` errors to all other unexpected
|
|
|
|
/// errors ocurring within the code handling the upstream search engines.
|
|
|
|
impl std::error::Error for EngineErrorKind {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
|
|
match self {
|
|
|
|
EngineErrorKind::RequestError(request_error) => Some(request_error),
|
|
|
|
EngineErrorKind::UnexpectedError { source, .. } => source.as_deref().map(|s| s),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-31 12:54:51 -04:00
|
|
|
|
|
|
|
/// Implementing `From` trait to map the `SelectorErrorKind` to `UnexpectedError` variant.
|
2023-06-04 04:56:07 -04:00
|
|
|
impl From<SelectorErrorKind<'_>> for EngineErrorKind {
|
|
|
|
fn from(err: SelectorErrorKind<'_>) -> Self {
|
|
|
|
Self::UnexpectedError {
|
|
|
|
message: err.to_string(),
|
|
|
|
source: None,
|
|
|
|
}
|
2023-05-31 12:54:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Implementing `From` trait to map the `InvalidHeaderValue` to `UnexpectedError` variant.
|
2023-06-01 05:21:45 -04:00
|
|
|
impl From<InvalidHeaderValue> for EngineErrorKind {
|
2023-05-31 12:54:51 -04:00
|
|
|
fn from(err: InvalidHeaderValue) -> Self {
|
2023-06-04 04:56:07 -04:00
|
|
|
Self::UnexpectedError {
|
|
|
|
message: err.to_string(),
|
|
|
|
source: Some(Box::new(err)),
|
|
|
|
}
|
2023-05-31 12:54:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Implementing `From` trait to map all `reqwest::Error` to `UnexpectedError` variant.
|
2023-06-01 05:21:45 -04:00
|
|
|
impl From<reqwest::Error> for EngineErrorKind {
|
2023-05-31 12:54:51 -04:00
|
|
|
fn from(err: reqwest::Error) -> Self {
|
2023-06-01 05:21:45 -04:00
|
|
|
Self::RequestError(err)
|
2023-05-31 12:54:51 -04:00
|
|
|
}
|
2023-05-16 05:22:00 -04:00
|
|
|
}
|