refactor(core): add ImageProcessingFailed error

This commit is contained in:
Paul Makles
2024-10-02 14:12:26 +01:00
parent bb202079e0
commit f31020fb6e
5 changed files with 16 additions and 12 deletions

View File

@@ -167,7 +167,7 @@ pub fn decode_image<R: Read + BufRead + Seek>(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<R: Read + BufRead + Seek>(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<R: Read + BufRead + Seek>(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<R: Read + BufRead + Seek>(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

View File

@@ -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()

View File

@@ -162,6 +162,7 @@ pub enum ErrorType {
max: usize,
},
FileTypeNotAllowed,
ImageProcessingFailed,
// ? Legacy errors
VosoUnavailable,

View File

@@ -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.