Replace UnsupportedFormat error with more helpful errors

This commit is contained in:
asonix 2023-07-09 15:07:49 -05:00
parent 74f4423d7b
commit 7dadbcf3ed
4 changed files with 17 additions and 9 deletions

View File

@ -90,8 +90,8 @@ pub(crate) enum UploadError {
#[error("Provided token did not match expected token")]
InvalidToken,
#[error("Unsupported image format")]
UnsupportedFormat,
#[error("Process endpoint was called with invalid extension")]
UnsupportedProcessExtension,
#[error("Gif uploads are not enabled")]
SilentVideoDisabled,
@ -168,11 +168,11 @@ impl ResponseError for Error {
| UploadError::Limit(_)
| UploadError::NoFiles
| UploadError::Upload(_)
| UploadError::UnsupportedFormat
| UploadError::Store(crate::store::StoreError::Repo(
crate::repo::RepoError::AlreadyClaimed,
))
| UploadError::Repo(crate::repo::RepoError::AlreadyClaimed)
| UploadError::UnsupportedProcessExtension
| UploadError::SilentVideoDisabled,
) => StatusCode::BAD_REQUEST,
Some(UploadError::MissingAlias) => StatusCode::NOT_FOUND,

View File

@ -4,7 +4,7 @@ mod tests;
use crate::{
config::{AudioCodec, ImageFormat, MediaConfiguration, VideoCodec},
error::{Error, UploadError},
magick::{Details, ValidInputType},
magick::{Details, ParseDetailsError, ValidInputType},
process::Process,
store::{Store, StoreError},
};
@ -498,7 +498,8 @@ fn parse_details(output: DetailsOutput) -> Result<Option<Details>, Error> {
stream.height,
stream.nb_read_frames.as_deref(),
*v,
);
)
.map_err(Error::from);
}
}
@ -510,9 +511,13 @@ fn parse_details_inner(
height: usize,
frames: Option<&str>,
format: VideoFormat,
) -> Result<Option<Details>, Error> {
) -> Result<Option<Details>, ParseDetailsError> {
let frames = frames
.map(|frames| frames.parse().map_err(|_| UploadError::UnsupportedFormat))
.map(|frames| {
frames
.parse()
.map_err(|_| ParseDetailsError::ParseFrames(String::from(frames)))
})
.transpose()?
.unwrap_or(1);

View File

@ -562,7 +562,7 @@ fn prepare_process(
let format = ext
.parse::<ImageFormat>()
.map_err(|_| UploadError::UnsupportedFormat)?;
.map_err(|_| UploadError::UnsupportedProcessExtension)?;
let ext = format.to_string();

View File

@ -272,6 +272,9 @@ pub(crate) enum ParseDetailsError {
#[error("Format is unsupported: {0}")]
Unsupported(String),
#[error("Could not parse frame count from {0}")]
ParseFrames(String),
}
fn parse_details(details_output: Vec<DetailsOutput>) -> Result<Details, Error> {
@ -297,7 +300,7 @@ fn parse_details(details_output: Vec<DetailsOutput>) -> Result<Details, Error> {
if !details_output
.iter()
.all(|details| &details.image.format == format)
.all(|details| details.image.format == format)
{
return Err(ParseDetailsError::MixedFormats.into());
}