Add ImgError struct for representing errors in downloading article images
This commit is contained in:
parent
910c45abf7
commit
a3de3fb6ff
3 changed files with 110 additions and 47 deletions
|
@ -14,6 +14,53 @@ pub enum ErrorKind {
|
||||||
ReadabilityError(String),
|
ReadabilityError(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
#[error("{kind}")]
|
||||||
|
/// Used to represent errors from downloading images. Errors from here are used solely for debugging
|
||||||
|
/// as they are considered recoverable.
|
||||||
|
pub struct ImgError {
|
||||||
|
kind: ErrorKind,
|
||||||
|
url: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ImgError {
|
||||||
|
pub fn with_kind(kind: ErrorKind) -> Self {
|
||||||
|
ImgError { url: None, kind }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_url(&mut self, url: &str) {
|
||||||
|
self.url = Some(url.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn url(&self) -> &Option<String> {
|
||||||
|
&self.url
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ErrorKind> for ImgError {
|
||||||
|
fn from(kind: ErrorKind) -> Self {
|
||||||
|
ImgError::with_kind(kind)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<surf::Error> for ImgError {
|
||||||
|
fn from(err: surf::Error) -> Self {
|
||||||
|
ImgError::with_kind(ErrorKind::HTTPError(err.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<url::ParseError> for ImgError {
|
||||||
|
fn from(err: url::ParseError) -> Self {
|
||||||
|
ImgError::with_kind(ErrorKind::HTTPError(err.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<std::io::Error> for ImgError {
|
||||||
|
fn from(err: std::io::Error) -> Self {
|
||||||
|
ImgError::with_kind(ErrorKind::IOError(err.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
#[error("{kind}")]
|
#[error("{kind}")]
|
||||||
pub struct PaperoniError {
|
pub struct PaperoniError {
|
||||||
|
|
21
src/http.rs
21
src/http.rs
|
@ -5,8 +5,8 @@ use indicatif::ProgressBar;
|
||||||
use log::{debug, info};
|
use log::{debug, info};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use crate::{errors::ErrorKind, errors::PaperoniError, extractor::Extractor};
|
use crate::errors::{ErrorKind, ImgError, PaperoniError};
|
||||||
|
use crate::extractor::Extractor;
|
||||||
type HTMLResource = (String, String);
|
type HTMLResource = (String, String);
|
||||||
|
|
||||||
pub async fn fetch_html(url: &str) -> Result<HTMLResource, PaperoniError> {
|
pub async fn fetch_html(url: &str) -> Result<HTMLResource, PaperoniError> {
|
||||||
|
@ -76,7 +76,7 @@ pub async fn download_images(
|
||||||
extractor: &mut Extractor,
|
extractor: &mut Extractor,
|
||||||
article_origin: &Url,
|
article_origin: &Url,
|
||||||
bar: &ProgressBar,
|
bar: &ProgressBar,
|
||||||
) -> Result<(), Vec<PaperoniError>> {
|
) -> Result<(), Vec<ImgError>> {
|
||||||
if extractor.img_urls.len() > 0 {
|
if extractor.img_urls.len() > 0 {
|
||||||
debug!(
|
debug!(
|
||||||
"Downloading {} images for {}",
|
"Downloading {} images for {}",
|
||||||
|
@ -102,7 +102,7 @@ pub async fn download_images(
|
||||||
bar.set_message(format!("Downloading images [{}/{}]", img_idx + 1, img_count).as_str());
|
bar.set_message(format!("Downloading images [{}/{}]", img_idx + 1, img_count).as_str());
|
||||||
match req.await {
|
match req.await {
|
||||||
Ok(mut img_response) => {
|
Ok(mut img_response) => {
|
||||||
// let mut img_response = req.await.expect("Unable to retrieve image");
|
let process_response = async {
|
||||||
let img_content: Vec<u8> = match img_response.body_bytes().await {
|
let img_content: Vec<u8> = match img_response.body_bytes().await {
|
||||||
Ok(bytes) => bytes,
|
Ok(bytes) => bytes,
|
||||||
Err(e) => return Err(e.into()),
|
Err(e) => return Err(e.into()),
|
||||||
|
@ -147,8 +147,17 @@ pub async fn download_images(
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
img_mime,
|
img_mime,
|
||||||
))
|
))
|
||||||
|
};
|
||||||
|
process_response.await.map_err(|mut e: ImgError| {
|
||||||
|
e.set_url(url);
|
||||||
|
e
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
let mut img_err: ImgError = e.into();
|
||||||
|
img_err.set_url(url);
|
||||||
|
Err(img_err)
|
||||||
}
|
}
|
||||||
Err(e) => Err(e.into()),
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -170,7 +179,7 @@ pub async fn download_images(
|
||||||
|
|
||||||
let imgs_req_iter = stream::from_iter(imgs_req_iter)
|
let imgs_req_iter = stream::from_iter(imgs_req_iter)
|
||||||
.buffered(10)
|
.buffered(10)
|
||||||
.collect::<Vec<Result<_, PaperoniError>>>()
|
.collect::<Vec<Result<_, ImgError>>>()
|
||||||
.await;
|
.await;
|
||||||
let mut errors = Vec::new();
|
let mut errors = Vec::new();
|
||||||
let mut replaced_imgs = Vec::new();
|
let mut replaced_imgs = Vec::new();
|
||||||
|
|
|
@ -70,6 +70,13 @@ fn download(app_config: AppConfig) {
|
||||||
if img_errors.len() > 1 { "s" } else { "" },
|
if img_errors.len() > 1 { "s" } else { "" },
|
||||||
url
|
url
|
||||||
);
|
);
|
||||||
|
for img_error in img_errors {
|
||||||
|
warn!(
|
||||||
|
"{}\n\t\tReason {}",
|
||||||
|
img_error.url().as_ref().unwrap(),
|
||||||
|
img_error
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
articles.push(extractor);
|
articles.push(extractor);
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue