Skip to content

Commit

Permalink
backend: Image: Add more detailed errors to logs system
Browse files Browse the repository at this point in the history
Including request errors, and social constraints like image size,
dimensions and format.

Closes #35.
  • Loading branch information
rafaelmardojai committed Mar 7, 2023
1 parent ebfa64a commit 4b82e6f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 20 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ once_cell = "1.5"
url = "2.2"
scraper = "0.12"
surf = "2.2"
async-std = "1.12"
async-std = "1.12"
human_bytes = { version = "0.4", default-features = false }
26 changes: 21 additions & 5 deletions src/backend/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::vec_of_strings;
use super::{
Data,
Image,
ImageError,
Log,
LogLevel,
Social,
Expand Down Expand Up @@ -271,7 +272,7 @@ impl Card {

if occurrences.len() > 0 {
logger.log(LogLevel::Debug, gettext!(
"Looking for valid occurrence for \"{}\"", name
"Looking for valid occurrences for \"{}\"", name
));
}

Expand All @@ -298,10 +299,25 @@ impl Card {
}
}
Err(err) => {
logger.log(LogLevel::Debug, gettext!(
"Image \"{}\" did not meet the requirements: {}.",
image.url, err
));
match err {
ImageError::RequestError(_) => {
logger.log(LogLevel::Error, format!(
"{}: \"{}\".", err, image.url
));
},
ImageError::TooHeavy{..} | ImageError::Unsupported(_) => {
logger.log(LogLevel::Warning, gettext!(
"{}: Image \"{}\" did not meet the requirements: {}.",
social, image.url, err
));
},
_ => {
logger.log(LogLevel::Debug, gettext!(
"{}: Image \"{}\" did not meet the requirements: {}.",
social, image.url, err
));
}
}
}
}
}
Expand Down
61 changes: 47 additions & 14 deletions src/backend/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::{
io::Cursor,
};

use human_bytes::human_bytes;
use image;
use url::{Url, ParseError};

Expand Down Expand Up @@ -82,7 +83,7 @@ impl Image {
self.bytes.replace(Some(bytes));
Ok(self.bytes.borrow().clone().unwrap())
} else {
Err(ImageError::Unexpected)
Err(ImageError::RequestError(resp.status().canonical_reason()))
}
}
}
Expand All @@ -92,12 +93,11 @@ impl Image {
&self,
social: &Social,
kinds: &Vec<SocialImageSizeKind>,
_constraints: &SocialConstraints
constraints: &SocialConstraints
) -> Result<SocialImageSizeKind, ImageError> {
if let (None, None) = (self.width.get(), self.height.get()) {

let bytes = self.fetch().await?;

if let (None, None) = (self.width.get(), self.height.get()) {
let (width, height) = async_std::task::spawn_blocking( move || -> Result<(u32, u32), ImageError> {
let image = image::load_from_memory(&bytes)?;
Ok((image.width(), image.height()))
Expand All @@ -108,6 +108,21 @@ impl Image {
self.height.set(Some(height));
}

if let Some(size) = self.size.get() {
if size > constraints.image_size {
return Err(ImageError::TooHeavy{
actual: human_bytes(size as f64),
max: human_bytes(constraints.image_size as f64)
});
}
}

if let Some(format) = self.format.get() {
if !constraints.image_formats.contains(&format) {
return Err(ImageError::Unsupported("Format is unsupported".to_string()));
}
}

if let (Some(width), Some(height)) = (self.width.get(), self.height.get()) {
for kind in kinds.iter() {
let (min_width, min_height) = social.image_size(kind);
Expand All @@ -121,7 +136,10 @@ impl Image {
None => (0, 0)
};

Err(ImageError::TooTiny(format!("{}x{}", sizes.0, sizes.1)))
Err(ImageError::TooTiny{
actual: format!("{}×{}px", width, height),
min: format!("{}×{}px", sizes.0, sizes.1)
})
} else {
Err(ImageError::Unexpected)
}
Expand Down Expand Up @@ -158,22 +176,37 @@ impl Image {
#[derive(Debug)]
pub enum ImageError {
FetchError(surf::Error),
RequestError(&'static str),
ImageError(image::error::ImageError),
TooTiny(String),
TooHeavy,
Unsupported,
TooTiny{
actual: String,
min: String
},
TooHeavy{
actual: String,
max: String
},
Unsupported(String),
Unexpected,
}

impl Display for ImageError {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
match *self {
ImageError::FetchError(ref e) => write!(f, "NetworkError: {}", e),
ImageError::ImageError(ref e) => write!(f, "ImageError: {}", e),
ImageError::TooTiny(ref s) => write!(f, "Image is too tiny, minimum size is {}", s),
ImageError::TooHeavy => write!(f, "TooHeavy"),
ImageError::Unsupported => write!(f, "Unsupported"),
ImageError::Unexpected => write!(f, "UnexpectedError"),
ImageError::FetchError(ref e) =>
write!(f, "Network Error: {}", e),
ImageError::RequestError(ref s) =>
write!(f, "Request Error: {}", s),
ImageError::ImageError(ref e) =>
write!(f, "Image Error: {}", e),
ImageError::TooTiny{ref actual, ref min} =>
write!(f, "Image is too tiny ({}), minimum dimensions are {}", actual, min),
ImageError::TooHeavy{ref actual, ref max} =>
write!(f, "Images is too heavy ({}), max size is {}", actual, max),
ImageError::Unsupported(ref s) =>
write!(f, "Images is unsupported: {}", s),
ImageError::Unexpected =>
write!(f, "Unexpected Error"),
}
}
}
Expand Down

0 comments on commit 4b82e6f

Please sign in to comment.