Skip to content

Commit

Permalink
chore(page): fix build reqwest error_for_status cache flag
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Jan 8, 2025
1 parent 77ec77e commit e39f5db
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 21 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spider/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider"
version = "2.24.11"
version = "2.24.12"
authors = [
"j-mendez <jeff@spider.cloud>"
]
Expand Down
33 changes: 26 additions & 7 deletions spider/src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::compact_str::CompactString;
#[cfg(all(feature = "chrome", not(feature = "decentralized")))]
use crate::configuration::{AutomationScripts, ExecutionScripts};
use crate::utils::abs::{convert_abs_path, convert_abs_url_base};
use crate::utils::PageResponse;
use crate::utils::{PageResponse, RequestError};
use crate::CaseInsensitiveString;
use crate::Client;
use crate::RelativeSelectors;
Expand Down Expand Up @@ -441,10 +441,12 @@ fn should_attempt_retry(error: &(dyn std::error::Error + 'static)) -> bool {
false
}


/// Get the error status of the page base.
#[cfg(not(feature = "decentralized"))]
fn get_error_status_base(should_retry: &mut bool, error_for_status: Option<Result<reqwest::Response, reqwest::Error>> ) -> Option<reqwest::Error> {
fn get_error_status_base(
should_retry: &mut bool,
error_for_status: Option<Result<reqwest::Response, RequestError>>,
) -> Option<RequestError> {
match error_for_status {
Some(e) => match e {
Ok(_) => None,
Expand All @@ -462,15 +464,24 @@ fn get_error_status_base(should_retry: &mut bool, error_for_status: Option<Resul
}
}

#[cfg(all(not(feature = "page_error_status_details"), not(feature = "decentralized")))]
#[cfg(all(
not(feature = "page_error_status_details"),
not(feature = "decentralized")
))]
/// Get the error status of the page.
fn get_error_status(should_retry: &mut bool, error_for_status: Option<Result<reqwest::Response, reqwest::Error>>) -> Option<String> {
fn get_error_status(
should_retry: &mut bool,
error_for_status: Option<Result<reqwest::Response, RequestError>>,
) -> Option<String> {
get_error_status_base(should_retry, error_for_status).map(|e| e.to_string())
}

#[cfg(all(feature = "page_error_status_details", not(feature = "decentralized")))]
/// Get the error status of the page.
fn get_error_status(should_retry: &mut bool, error_for_status: Option<Result<reqwest::Response, reqwest::Error>> ) -> Option<std::sync::Arc<reqwest::Error>> {
fn get_error_status(
should_retry: &mut bool,
error_for_status: Option<Result<reqwest::Response, RequestError>>,
) -> Option<std::sync::Arc<reqwest::Error>> {
get_error_status_base(should_retry, error_for_status).map(std::sync::Arc::new)
}

Expand All @@ -493,6 +504,7 @@ pub fn build(url: &str, res: PageResponse) -> Page {
remote_addr: res.remote_addr,
#[cfg(feature = "cookies")]
cookies: res.cookies,
// we do not need to parse the Url on each page. Next version this should be set to None.
base: if !url.is_empty() {
match Url::parse(url) {
Ok(u) => Some(u),
Expand Down Expand Up @@ -869,7 +881,6 @@ impl Page {
page_response
}
};

build(url, page_response)
}

Expand Down Expand Up @@ -1207,6 +1218,14 @@ impl Page {
self.url = url;
}

/// Set the url directly parsed url of the page. Useful for transforming the content and rewriting the url.
#[cfg(not(feature = "decentralized"))]
pub fn set_url_parsed_direct(&mut self) {
if let Ok(base) = Url::parse(&self.url) {
self.base = Some(base);
}
}

/// Set the url directly parsed url of the page. Useful for transforming the content and rewriting the url.
#[cfg(not(feature = "decentralized"))]
pub fn set_url_parsed(&mut self, url_parsed: Url) {
Expand Down
12 changes: 10 additions & 2 deletions spider/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ use log::{info, log_enabled, Level};

use reqwest::{
header::{HeaderName, HeaderValue},
Error, Response, StatusCode,
Response, StatusCode,
};

/// The request error.
#[cfg(not(feature = "cache_request"))]
pub(crate) type RequestError = reqwest::Error;

/// The request error.
#[cfg(feature = "cache_request")]
pub(crate) type RequestError = reqwest_middleware::Error;

/// Ignore the content types.
pub static IGNORE_CONTENT_TYPES: phf::Set<&'static str> = phf_set! {
"application/pdf",
Expand Down Expand Up @@ -206,7 +214,7 @@ pub struct PageResponse {
/// The final url destination after any redirects.
pub final_url: Option<String>,
/// The message of the response error if any.
pub error_for_status: Option<Result<Response, Error>>,
pub error_for_status: Option<Result<Response, RequestError>>,
#[cfg(feature = "chrome")]
/// The screenshot bytes of the page. The ScreenShotConfig bytes boolean needs to be set to true.
pub screenshot_bytes: Option<Vec<u8>>,
Expand Down
2 changes: 1 addition & 1 deletion spider_chrome/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_chrome"
version = "2.24.11"
version = "2.24.12"
rust-version = "1.70"
authors = [
"j-mendez <jeff@spider.cloud>"
Expand Down
2 changes: 1 addition & 1 deletion spider_cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_cli"
version = "2.24.11"
version = "2.24.12"
authors = [
"j-mendez <jeff@spider.cloud>"
]
Expand Down
2 changes: 1 addition & 1 deletion spider_transformations/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_transformations"
version = "2.24.11"
version = "2.24.12"
authors = [
"j-mendez <jeff@spider.cloud>"
]
Expand Down
2 changes: 1 addition & 1 deletion spider_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_utils"
version = "2.24.11"
version = "2.24.12"
authors = [
"j-mendez <jeff@spider.cloud>"
]
Expand Down
2 changes: 1 addition & 1 deletion spider_worker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "spider_worker"
version = "2.24.11"
version = "2.24.12"
authors = [
"j-mendez <jeff@spider.cloud>"
]
Expand Down

0 comments on commit e39f5db

Please sign in to comment.