From f31020fb6ed3df219d3d88789a13d581f623dda7 Mon Sep 17 00:00:00 2001 From: Paul Makles Date: Wed, 2 Oct 2024 14:12:26 +0100 Subject: [PATCH] refactor(core): add ImageProcessingFailed error --- crates/core/files/src/lib.rs | 12 ++++++------ crates/core/result/src/axum.rs | 2 ++ crates/core/result/src/lib.rs | 1 + crates/core/result/src/rocket.rs | 1 + crates/services/january/src/requests.rs | 12 ++++++------ 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/crates/core/files/src/lib.rs b/crates/core/files/src/lib.rs index 6ad905582..b1ff75ddb 100644 --- a/crates/core/files/src/lib.rs +++ b/crates/core/files/src/lib.rs @@ -167,7 +167,7 @@ pub fn decode_image(reader: &mut R, mime: &str) -> Res jxl_image.height(), frame.image().buf().to_vec(), ) - .ok_or_else(|| create_error!(LabelMe))?, + .ok_or_else(|| create_error!(ImageProcessingFailed))?, ) .to_rgb8(), )), @@ -178,14 +178,14 @@ pub fn decode_image(reader: &mut R, mime: &str) -> Res jxl_image.height(), frame.image().buf().to_vec(), ) - .ok_or_else(|| create_error!(LabelMe))?, + .ok_or_else(|| create_error!(LabeImageProcessingFailedlMe))?, ) .to_rgba8(), )), - _ => Err(create_error!(LabelMe)), + _ => Err(create_error!(ImageProcessingFailed)), } } else { - Err(create_error!(LabelMe)) + Err(create_error!(ImageProcessingFailed)) } } // Read image using resvg @@ -197,7 +197,7 @@ pub fn decode_image(reader: &mut R, mime: &str) -> Res let tree = report_internal_error!(usvg::Tree::from_data(&buf, &Default::default()))?; let size = tree.size(); let mut pixmap = Pixmap::new(size.width() as u32, size.height() as u32) - .ok_or_else(|| create_error!(LabelMe))?; + .ok_or_else(|| create_error!(ImageProcessingFailed))?; let mut pixmap_mut = pixmap.as_mut(); resvg::render(&tree, Default::default(), &mut pixmap_mut); @@ -208,7 +208,7 @@ pub fn decode_image(reader: &mut R, mime: &str) -> Res size.height() as u32, pixmap.data().to_vec(), ) - .ok_or_else(|| create_error!(LabelMe))?, + .ok_or_else(|| create_error!(ImageProcessingFailed))?, )) } // Check if we can read using image-rs crate diff --git a/crates/core/result/src/axum.rs b/crates/core/result/src/axum.rs index 11f1fa1ce..dcf782406 100644 --- a/crates/core/result/src/axum.rs +++ b/crates/core/result/src/axum.rs @@ -1,4 +1,5 @@ use axum::{http::StatusCode, response::IntoResponse, Json}; +use rocket::http::Status; use crate::{Error, ErrorType}; @@ -78,6 +79,7 @@ impl IntoResponse for Error { ErrorType::FileTooSmall => StatusCode::UNPROCESSABLE_ENTITY, ErrorType::FileTooLarge { .. } => StatusCode::UNPROCESSABLE_ENTITY, ErrorType::FileTypeNotAllowed => StatusCode::BAD_REQUEST, + ErrorType::ImageProcessingFailed => StatusCode::INTERNAL_SERVER_ERROR, }; (status, Json(&self)).into_response() diff --git a/crates/core/result/src/lib.rs b/crates/core/result/src/lib.rs index 06ef7810e..0d6a03fd4 100644 --- a/crates/core/result/src/lib.rs +++ b/crates/core/result/src/lib.rs @@ -162,6 +162,7 @@ pub enum ErrorType { max: usize, }, FileTypeNotAllowed, + ImageProcessingFailed, // ? Legacy errors VosoUnavailable, diff --git a/crates/core/result/src/rocket.rs b/crates/core/result/src/rocket.rs index 6efcb663c..dfdbee49f 100644 --- a/crates/core/result/src/rocket.rs +++ b/crates/core/result/src/rocket.rs @@ -84,6 +84,7 @@ impl<'r> Responder<'r, 'static> for Error { ErrorType::FileTooSmall => Status::UnprocessableEntity, ErrorType::FileTooLarge { .. } => Status::UnprocessableEntity, ErrorType::FileTypeNotAllowed => Status::BadRequest, + ErrorType::ImageProcessingFailed => Status::InternalServerError, }; // Serialize the error data structure into JSON. diff --git a/crates/services/january/src/requests.rs b/crates/services/january/src/requests.rs index 5e6fbba67..b59cddf3e 100644 --- a/crates/services/january/src/requests.rs +++ b/crates/services/january/src/requests.rs @@ -66,7 +66,7 @@ impl Request { let Request { response, mime } = Request::new(url).await?; if matches!(mime.type_(), mime::IMAGE | mime::VIDEO) { - let bytes = response.bytes().await.map_err(|_| create_error!(LabelMe)); + let bytes = report_internal_error!(response.bytes().await)?; let result = match bytes { Ok(bytes) => { @@ -77,7 +77,7 @@ impl Request { if is_valid_image(reader, "image/gif") { Ok(("image/gif".to_owned(), bytes.to_vec())) } else { - Err(create_error!(LabelMe)) + Err(create_error!(FileTypeNotAllowed)) } } else { Ok(( @@ -96,7 +96,7 @@ impl Request { if video_size(&file).is_some() { Ok((mime.to_string(), bytes.to_vec())) } else { - Err(create_error!(LabelMe)) + Err(create_error!(FileTypeNotAllowed)) } } } @@ -106,7 +106,7 @@ impl Request { PROXY_CACHE.insert(url.to_owned(), result.clone()).await; result } else { - Err(create_error!(LabelMe)) + Err(create_error!(FileTypeNotAllowed)) } } } @@ -129,7 +129,7 @@ impl Request { if matches!(mime.type_(), mime::IMAGE) { response } else { - return Err(create_error!(LabelMe)); + return Err(create_error!(FileTypeNotAllowed)); } }; @@ -166,7 +166,7 @@ impl Request { if matches!(mime.type_(), mime::VIDEO) { response } else { - return Err(create_error!(LabelMe)); + return Err(create_error!(FileTypeNotAllowed)); } };