Spaces:
Running
Running
neon_arch
commited on
Commit
·
5962cca
1
Parent(s):
019b332
chore: provide a better and more standardized way to handle engine errors
Browse files- src/engines/duckduckgo.rs +14 -5
- src/engines/engine_models.rs +64 -6
- src/engines/searx.rs +18 -5
src/engines/duckduckgo.rs
CHANGED
|
@@ -9,6 +9,8 @@ use scraper::{Html, Selector};
|
|
| 9 |
|
| 10 |
use crate::search_results_handler::aggregation_models::RawSearchResult;
|
| 11 |
|
|
|
|
|
|
|
| 12 |
/// This function scrapes results from the upstream engine duckduckgo and puts all the scraped
|
| 13 |
/// results like title, visiting_url (href in html),engine (from which engine it was fetched from)
|
| 14 |
/// and description in a RawSearchResult and then adds that to HashMap whose keys are url and
|
|
@@ -22,14 +24,15 @@ use crate::search_results_handler::aggregation_models::RawSearchResult;
|
|
| 22 |
///
|
| 23 |
/// # Errors
|
| 24 |
///
|
| 25 |
-
/// Returns
|
| 26 |
-
/// reach the above `upstream search engine` page
|
| 27 |
-
///
|
|
|
|
| 28 |
pub async fn results(
|
| 29 |
query: &str,
|
| 30 |
page: u32,
|
| 31 |
user_agent: &str,
|
| 32 |
-
) -> Result<HashMap<String, RawSearchResult>,
|
| 33 |
// Page number can be missing or empty string and so appropriate handling is required
|
| 34 |
// so that upstream server recieves valid page number.
|
| 35 |
let url: String = match page {
|
|
@@ -54,7 +57,6 @@ pub async fn results(
|
|
| 54 |
header_map.insert(COOKIE, "kl=wt-wt".parse()?);
|
| 55 |
|
| 56 |
// fetch the html from upstream duckduckgo engine
|
| 57 |
-
// TODO: Write better error handling code to handle no results case.
|
| 58 |
let results: String = reqwest::Client::new()
|
| 59 |
.get(url)
|
| 60 |
.timeout(Duration::from_secs(30))
|
|
@@ -65,6 +67,13 @@ pub async fn results(
|
|
| 65 |
.await?;
|
| 66 |
|
| 67 |
let document: Html = Html::parse_document(&results);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
let results: Selector = Selector::parse(".result")?;
|
| 69 |
let result_title: Selector = Selector::parse(".result__a")?;
|
| 70 |
let result_url: Selector = Selector::parse(".result__url")?;
|
|
|
|
| 9 |
|
| 10 |
use crate::search_results_handler::aggregation_models::RawSearchResult;
|
| 11 |
|
| 12 |
+
use super::engine_models::EngineErrorKind;
|
| 13 |
+
|
| 14 |
/// This function scrapes results from the upstream engine duckduckgo and puts all the scraped
|
| 15 |
/// results like title, visiting_url (href in html),engine (from which engine it was fetched from)
|
| 16 |
/// and description in a RawSearchResult and then adds that to HashMap whose keys are url and
|
|
|
|
| 24 |
///
|
| 25 |
/// # Errors
|
| 26 |
///
|
| 27 |
+
/// Returns an `EngineErrorKind` if the user is not connected to the internet or if their is failure to
|
| 28 |
+
/// reach the above `upstream search engine` page or if the `upstream search engine` is unable to
|
| 29 |
+
/// provide results for the requested search query and also returns error if the scraping selector
|
| 30 |
+
/// or HeaderMap fails to initialize.
|
| 31 |
pub async fn results(
|
| 32 |
query: &str,
|
| 33 |
page: u32,
|
| 34 |
user_agent: &str,
|
| 35 |
+
) -> Result<HashMap<String, RawSearchResult>, EngineErrorKind> {
|
| 36 |
// Page number can be missing or empty string and so appropriate handling is required
|
| 37 |
// so that upstream server recieves valid page number.
|
| 38 |
let url: String = match page {
|
|
|
|
| 57 |
header_map.insert(COOKIE, "kl=wt-wt".parse()?);
|
| 58 |
|
| 59 |
// fetch the html from upstream duckduckgo engine
|
|
|
|
| 60 |
let results: String = reqwest::Client::new()
|
| 61 |
.get(url)
|
| 62 |
.timeout(Duration::from_secs(30))
|
|
|
|
| 67 |
.await?;
|
| 68 |
|
| 69 |
let document: Html = Html::parse_document(&results);
|
| 70 |
+
|
| 71 |
+
let no_result: Selector = Selector::parse(".no-results")?;
|
| 72 |
+
|
| 73 |
+
if let Some(_) = document.select(&no_result).next() {
|
| 74 |
+
return Err(EngineErrorKind::EmptyResultSet);
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
let results: Selector = Selector::parse(".result")?;
|
| 78 |
let result_title: Selector = Selector::parse(".result__a")?;
|
| 79 |
let result_url: Selector = Selector::parse(".result__url")?;
|
src/engines/engine_models.rs
CHANGED
|
@@ -1,8 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
#[derive(Debug)]
|
| 2 |
-
pub enum
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
}
|
|
|
|
| 1 |
+
//! This module provides the error enum to handle different errors associated while requesting data from
|
| 2 |
+
//! the upstream search engines with the search query provided by the user.
|
| 3 |
+
|
| 4 |
+
use reqwest::header::InvalidHeaderValue;
|
| 5 |
+
use scraper::error::SelectorErrorKind;
|
| 6 |
+
|
| 7 |
+
/// A custom error type used for handle engine associated errors.
|
| 8 |
+
///
|
| 9 |
+
/// This enum provides variants three different categories of errors:
|
| 10 |
+
/// * `RequestError` - This variant handles all request related errors like forbidden, not found,
|
| 11 |
+
/// etc.
|
| 12 |
+
/// * `EmptyResultSet` - This variant handles the not results found error provide by the upstream
|
| 13 |
+
/// search engines.
|
| 14 |
+
/// * `UnexpectedError` - This variant handles all the errors which are unexpected or occur rarely
|
| 15 |
+
/// and are errors mostly related to failure in initialization of HeaderMap, Selector errors and
|
| 16 |
+
/// all other errors.
|
| 17 |
#[derive(Debug)]
|
| 18 |
+
pub enum EngineErrorKind {
|
| 19 |
+
RequestError(reqwest::Error),
|
| 20 |
+
EmptyResultSet,
|
| 21 |
+
UnexpectedError(String),
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
/// Implementing `Display` trait to make errors writable on the stdout and also providing/passing the
|
| 25 |
+
/// appropriate errors that should be written to the stdout when this error is raised/encountered.
|
| 26 |
+
impl std::fmt::Display for EngineErrorKind {
|
| 27 |
+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
| 28 |
+
match self {
|
| 29 |
+
EngineErrorKind::RequestError(request_error) => write!(f, "{}", request_error),
|
| 30 |
+
EngineErrorKind::EmptyResultSet => {
|
| 31 |
+
write!(f, "The upstream search engine returned an empty result set")
|
| 32 |
+
}
|
| 33 |
+
EngineErrorKind::UnexpectedError(unexpected_error) => write!(f, "{}", unexpected_error),
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
/// Implementing `Error` trait to make the the `EngineErrorKind` enum an error type.
|
| 39 |
+
impl std::error::Error for EngineErrorKind {}
|
| 40 |
+
|
| 41 |
+
/// Implementing `From` trait to map the `SelectorErrorKind` to `UnexpectedError` variant.
|
| 42 |
+
impl<'a> From<SelectorErrorKind<'a>> for EngineErrorKind {
|
| 43 |
+
fn from(err: SelectorErrorKind<'a>) -> Self {
|
| 44 |
+
match err {
|
| 45 |
+
_ => Self::UnexpectedError(err.to_string()),
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
/// Implementing `From` trait to map the `InvalidHeaderValue` to `UnexpectedError` variant.
|
| 51 |
+
impl<'a> From<InvalidHeaderValue> for EngineErrorKind {
|
| 52 |
+
fn from(err: InvalidHeaderValue) -> Self {
|
| 53 |
+
match err {
|
| 54 |
+
_ => Self::UnexpectedError(err.to_string()),
|
| 55 |
+
}
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
/// Implementing `From` trait to map all `reqwest::Error` to `UnexpectedError` variant.
|
| 60 |
+
impl<'a> From<reqwest::Error> for EngineErrorKind {
|
| 61 |
+
fn from(err: reqwest::Error) -> Self {
|
| 62 |
+
match err {
|
| 63 |
+
_ => Self::RequestError(err),
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
}
|
src/engines/searx.rs
CHANGED
|
@@ -8,6 +8,8 @@ use std::collections::HashMap;
|
|
| 8 |
|
| 9 |
use crate::search_results_handler::aggregation_models::RawSearchResult;
|
| 10 |
|
|
|
|
|
|
|
| 11 |
/// This function scrapes results from the upstream engine duckduckgo and puts all the scraped
|
| 12 |
/// results like title, visiting_url (href in html),engine (from which engine it was fetched from)
|
| 13 |
/// and description in a RawSearchResult and then adds that to HashMap whose keys are url and
|
|
@@ -21,14 +23,15 @@ use crate::search_results_handler::aggregation_models::RawSearchResult;
|
|
| 21 |
///
|
| 22 |
/// # Errors
|
| 23 |
///
|
| 24 |
-
/// Returns
|
| 25 |
-
/// reach the above `upstream search engine` page
|
| 26 |
-
///
|
|
|
|
| 27 |
pub async fn results(
|
| 28 |
query: &str,
|
| 29 |
page: u32,
|
| 30 |
user_agent: &str,
|
| 31 |
-
) -> Result<HashMap<String, RawSearchResult>,
|
| 32 |
// Page number can be missing or empty string and so appropriate handling is required
|
| 33 |
// so that upstream server recieves valid page number.
|
| 34 |
let url: String = format!("https://searx.work/search?q={query}&pageno={page}");
|
|
@@ -41,7 +44,6 @@ pub async fn results(
|
|
| 41 |
header_map.insert(COOKIE, "categories=general; language=auto; locale=en; autocomplete=duckduckgo; image_proxy=1; method=POST; safesearch=2; theme=simple; results_on_new_tab=1; doi_resolver=oadoi.org; simple_style=auto; center_alignment=1; query_in_title=1; infinite_scroll=0; disabled_engines=; enabled_engines=\"archive is__general\\054yep__general\\054curlie__general\\054currency__general\\054ddg definitions__general\\054wikidata__general\\054duckduckgo__general\\054tineye__general\\054lingva__general\\054startpage__general\\054yahoo__general\\054wiby__general\\054marginalia__general\\054alexandria__general\\054wikibooks__general\\054wikiquote__general\\054wikisource__general\\054wikiversity__general\\054wikivoyage__general\\054dictzone__general\\054seznam__general\\054mojeek__general\\054naver__general\\054wikimini__general\\054brave__general\\054petalsearch__general\\054goo__general\"; disabled_plugins=; enabled_plugins=\"searx.plugins.hostname_replace\\054searx.plugins.oa_doi_rewrite\\054searx.plugins.vim_hotkeys\"; tokens=; maintab=on; enginetab=on".parse()?);
|
| 42 |
|
| 43 |
// fetch the html from upstream searx instance engine
|
| 44 |
-
// TODO: Write better error handling code to handle no results case.
|
| 45 |
let results: String = reqwest::Client::new()
|
| 46 |
.get(url)
|
| 47 |
.headers(header_map) // add spoofed headers to emulate human behaviours.
|
|
@@ -51,6 +53,17 @@ pub async fn results(
|
|
| 51 |
.await?;
|
| 52 |
|
| 53 |
let document: Html = Html::parse_document(&results);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
let results: Selector = Selector::parse(".result")?;
|
| 55 |
let result_title: Selector = Selector::parse("h3>a")?;
|
| 56 |
let result_url: Selector = Selector::parse("h3>a")?;
|
|
|
|
| 8 |
|
| 9 |
use crate::search_results_handler::aggregation_models::RawSearchResult;
|
| 10 |
|
| 11 |
+
use super::engine_models::EngineErrorKind;
|
| 12 |
+
|
| 13 |
/// This function scrapes results from the upstream engine duckduckgo and puts all the scraped
|
| 14 |
/// results like title, visiting_url (href in html),engine (from which engine it was fetched from)
|
| 15 |
/// and description in a RawSearchResult and then adds that to HashMap whose keys are url and
|
|
|
|
| 23 |
///
|
| 24 |
/// # Errors
|
| 25 |
///
|
| 26 |
+
/// Returns an `EngineErrorKind` if the user is not connected to the internet or if their is failure to
|
| 27 |
+
/// reach the above `upstream search engine` page or if the `upstream search engine` is unable to
|
| 28 |
+
/// provide results for the requested search query and also returns error if the scraping selector
|
| 29 |
+
/// or HeaderMap fails to initialize.
|
| 30 |
pub async fn results(
|
| 31 |
query: &str,
|
| 32 |
page: u32,
|
| 33 |
user_agent: &str,
|
| 34 |
+
) -> Result<HashMap<String, RawSearchResult>, EngineErrorKind> {
|
| 35 |
// Page number can be missing or empty string and so appropriate handling is required
|
| 36 |
// so that upstream server recieves valid page number.
|
| 37 |
let url: String = format!("https://searx.work/search?q={query}&pageno={page}");
|
|
|
|
| 44 |
header_map.insert(COOKIE, "categories=general; language=auto; locale=en; autocomplete=duckduckgo; image_proxy=1; method=POST; safesearch=2; theme=simple; results_on_new_tab=1; doi_resolver=oadoi.org; simple_style=auto; center_alignment=1; query_in_title=1; infinite_scroll=0; disabled_engines=; enabled_engines=\"archive is__general\\054yep__general\\054curlie__general\\054currency__general\\054ddg definitions__general\\054wikidata__general\\054duckduckgo__general\\054tineye__general\\054lingva__general\\054startpage__general\\054yahoo__general\\054wiby__general\\054marginalia__general\\054alexandria__general\\054wikibooks__general\\054wikiquote__general\\054wikisource__general\\054wikiversity__general\\054wikivoyage__general\\054dictzone__general\\054seznam__general\\054mojeek__general\\054naver__general\\054wikimini__general\\054brave__general\\054petalsearch__general\\054goo__general\"; disabled_plugins=; enabled_plugins=\"searx.plugins.hostname_replace\\054searx.plugins.oa_doi_rewrite\\054searx.plugins.vim_hotkeys\"; tokens=; maintab=on; enginetab=on".parse()?);
|
| 45 |
|
| 46 |
// fetch the html from upstream searx instance engine
|
|
|
|
| 47 |
let results: String = reqwest::Client::new()
|
| 48 |
.get(url)
|
| 49 |
.headers(header_map) // add spoofed headers to emulate human behaviours.
|
|
|
|
| 53 |
.await?;
|
| 54 |
|
| 55 |
let document: Html = Html::parse_document(&results);
|
| 56 |
+
|
| 57 |
+
let no_result: Selector = Selector::parse("#urls>.dialog-error>p")?;
|
| 58 |
+
|
| 59 |
+
if let Some(no_result_msg) = document.select(&no_result).nth(1) {
|
| 60 |
+
if no_result_msg.inner_html()
|
| 61 |
+
== "we didn't find any results. Please use another query or search in more categories"
|
| 62 |
+
{
|
| 63 |
+
return Err(EngineErrorKind::EmptyResultSet);
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
let results: Selector = Selector::parse(".result")?;
|
| 68 |
let result_title: Selector = Selector::parse("h3>a")?;
|
| 69 |
let result_url: Selector = Selector::parse("h3>a")?;
|