2021-06-19 19:39:41 +00:00
|
|
|
use actix_web::{http::StatusCode, HttpResponse, ResponseError};
|
2022-03-29 01:47:46 +00:00
|
|
|
use color_eyre::Report;
|
2021-09-14 01:22:42 +00:00
|
|
|
|
|
|
|
pub(crate) struct Error {
|
2022-03-29 01:47:46 +00:00
|
|
|
inner: color_eyre::Report,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error {
|
|
|
|
fn kind(&self) -> Option<&UploadError> {
|
|
|
|
self.inner.downcast_ref()
|
|
|
|
}
|
2023-07-16 17:21:46 +00:00
|
|
|
|
|
|
|
pub(crate) fn root_cause(&self) -> &(dyn std::error::Error + 'static) {
|
|
|
|
self.inner.root_cause()
|
|
|
|
}
|
2021-09-14 01:22:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Error {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2022-03-29 01:47:46 +00:00
|
|
|
std::fmt::Debug::fmt(&self.inner, f)
|
2021-09-14 01:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2022-03-29 01:47:46 +00:00
|
|
|
std::fmt::Display::fmt(&self.inner, f)
|
2021-09-14 01:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for Error {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
2022-03-29 01:47:46 +00:00
|
|
|
self.inner.source()
|
2021-09-14 01:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> From<T> for Error
|
|
|
|
where
|
|
|
|
UploadError: From<T>,
|
|
|
|
{
|
|
|
|
fn from(error: T) -> Self {
|
|
|
|
Error {
|
2022-03-29 01:47:46 +00:00
|
|
|
inner: Report::from(UploadError::from(error)),
|
2021-09-14 01:22:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-06 21:41:17 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
2020-06-11 16:46:00 +00:00
|
|
|
pub(crate) enum UploadError {
|
2023-06-23 16:20:55 +00:00
|
|
|
#[error("Couldn't upload file")]
|
2021-09-14 01:50:51 +00:00
|
|
|
Upload(#[from] actix_form_data::Error),
|
2020-06-06 21:41:17 +00:00
|
|
|
|
2022-03-26 21:49:23 +00:00
|
|
|
#[error("Error in DB")]
|
2023-06-20 20:59:08 +00:00
|
|
|
Repo(#[from] crate::repo::RepoError),
|
2022-03-26 21:49:23 +00:00
|
|
|
|
|
|
|
#[error("Error in old sled DB")]
|
|
|
|
OldSled(#[from] ::sled::Error),
|
2020-06-06 21:41:17 +00:00
|
|
|
|
2022-03-26 21:49:23 +00:00
|
|
|
#[error("Error parsing string")]
|
2020-06-06 21:41:17 +00:00
|
|
|
ParseString(#[from] std::string::FromUtf8Error),
|
|
|
|
|
2022-03-26 21:49:23 +00:00
|
|
|
#[error("Error interacting with filesystem")]
|
2020-06-07 00:29:15 +00:00
|
|
|
Io(#[from] std::io::Error),
|
|
|
|
|
2022-03-26 21:49:23 +00:00
|
|
|
#[error("Error generating path")]
|
2021-10-19 01:29:06 +00:00
|
|
|
PathGenerator(#[from] storage_path_generator::PathError),
|
|
|
|
|
2022-03-26 21:49:23 +00:00
|
|
|
#[error("Error stripping prefix")]
|
2021-10-19 01:29:06 +00:00
|
|
|
StripPrefix(#[from] std::path::StripPrefixError),
|
|
|
|
|
2023-06-20 20:59:08 +00:00
|
|
|
#[error("Error in store")]
|
|
|
|
Store(#[source] crate::store::StoreError),
|
2021-10-28 04:06:03 +00:00
|
|
|
|
2023-07-10 20:29:41 +00:00
|
|
|
#[error("Error in ffmpeg")]
|
|
|
|
Ffmpeg(#[from] crate::ffmpeg::FfMpegError),
|
|
|
|
|
|
|
|
#[error("Error in imagemagick")]
|
|
|
|
Magick(#[from] crate::magick::MagickError),
|
|
|
|
|
|
|
|
#[error("Error in exiftool")]
|
|
|
|
Exiftool(#[from] crate::exiftool::ExifError),
|
2023-07-09 19:50:58 +00:00
|
|
|
|
2023-07-21 21:21:24 +00:00
|
|
|
#[error("Error building reqwest client")]
|
|
|
|
BuildClient(#[source] reqwest::Error),
|
|
|
|
|
|
|
|
#[error("Error making request")]
|
|
|
|
ReqwestMiddleware(#[from] reqwest_middleware::Error),
|
|
|
|
|
|
|
|
#[error("Error in request response")]
|
|
|
|
Reqwest(#[from] reqwest::Error),
|
|
|
|
|
2021-10-21 02:36:18 +00:00
|
|
|
#[error("Provided process path is invalid")]
|
|
|
|
ParsePath,
|
|
|
|
|
2021-09-04 21:01:48 +00:00
|
|
|
#[error("Failed to acquire the semaphore")]
|
|
|
|
Semaphore,
|
|
|
|
|
2020-06-06 21:41:17 +00:00
|
|
|
#[error("Panic in blocking operation")]
|
|
|
|
Canceled,
|
|
|
|
|
|
|
|
#[error("No files present in upload")]
|
|
|
|
NoFiles,
|
|
|
|
|
2020-06-06 22:43:33 +00:00
|
|
|
#[error("Requested a file that doesn't exist")]
|
|
|
|
MissingAlias,
|
|
|
|
|
2023-07-07 18:17:26 +00:00
|
|
|
#[error("Requested a file that pict-rs lost track of")]
|
|
|
|
MissingIdentifier,
|
|
|
|
|
2020-06-07 00:29:15 +00:00
|
|
|
#[error("Provided token did not match expected token")]
|
|
|
|
InvalidToken,
|
2020-06-07 01:44:26 +00:00
|
|
|
|
2023-07-09 20:07:49 +00:00
|
|
|
#[error("Process endpoint was called with invalid extension")]
|
|
|
|
UnsupportedProcessExtension,
|
2020-06-07 15:59:58 +00:00
|
|
|
|
2022-03-29 16:04:56 +00:00
|
|
|
#[error("Gif uploads are not enabled")]
|
|
|
|
SilentVideoDisabled,
|
|
|
|
|
2020-06-07 15:59:58 +00:00
|
|
|
#[error("Unable to download image, bad response {0}")]
|
|
|
|
Download(actix_web::http::StatusCode),
|
|
|
|
|
2020-06-11 00:26:54 +00:00
|
|
|
#[error("Tried to save an image with an already-taken name")]
|
|
|
|
DuplicateAlias,
|
2020-06-11 16:46:00 +00:00
|
|
|
|
2022-03-26 21:49:23 +00:00
|
|
|
#[error("Error in json")]
|
2020-12-10 05:05:04 +00:00
|
|
|
Json(#[from] serde_json::Error),
|
2021-01-14 13:52:11 +00:00
|
|
|
|
|
|
|
#[error("Range header not satisfiable")]
|
|
|
|
Range,
|
2021-08-28 22:15:14 +00:00
|
|
|
|
2022-03-26 21:49:23 +00:00
|
|
|
#[error("Hit limit")]
|
2022-03-29 20:59:17 +00:00
|
|
|
Limit(#[from] crate::stream::LimitError),
|
2022-03-29 21:48:26 +00:00
|
|
|
|
|
|
|
#[error("Response timeout")]
|
|
|
|
Timeout(#[from] crate::stream::TimeoutError),
|
2023-09-06 23:53:52 +00:00
|
|
|
|
2023-12-04 21:36:23 +00:00
|
|
|
#[error("Client took too long to send request")]
|
|
|
|
AggregateTimeout,
|
|
|
|
|
2023-09-06 23:53:52 +00:00
|
|
|
#[error("Failed external validation")]
|
|
|
|
ExternalValidation,
|
2020-06-07 15:59:58 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 22:57:42 +00:00
|
|
|
impl From<actix_web::error::BlockingError> for UploadError {
|
|
|
|
fn from(_: actix_web::error::BlockingError) -> Self {
|
|
|
|
UploadError::Canceled
|
2020-06-06 21:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 21:01:48 +00:00
|
|
|
impl From<tokio::sync::AcquireError> for UploadError {
|
|
|
|
fn from(_: tokio::sync::AcquireError) -> Self {
|
|
|
|
UploadError::Semaphore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 20:59:08 +00:00
|
|
|
impl From<crate::store::StoreError> for UploadError {
|
|
|
|
fn from(value: crate::store::StoreError) -> Self {
|
|
|
|
match value {
|
|
|
|
crate::store::StoreError::Repo(repo_error) => Self::Repo(repo_error),
|
|
|
|
e => Self::Store(e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 01:22:42 +00:00
|
|
|
impl ResponseError for Error {
|
2020-06-06 21:41:17 +00:00
|
|
|
fn status_code(&self) -> StatusCode {
|
2022-03-29 01:47:46 +00:00
|
|
|
match self.kind() {
|
|
|
|
Some(
|
|
|
|
UploadError::DuplicateAlias
|
|
|
|
| UploadError::Limit(_)
|
|
|
|
| UploadError::NoFiles
|
2022-03-29 16:04:56 +00:00
|
|
|
| UploadError::Upload(_)
|
2023-06-20 20:59:08 +00:00
|
|
|
| UploadError::Store(crate::store::StoreError::Repo(
|
|
|
|
crate::repo::RepoError::AlreadyClaimed,
|
|
|
|
))
|
|
|
|
| UploadError::Repo(crate::repo::RepoError::AlreadyClaimed)
|
2023-07-09 20:07:49 +00:00
|
|
|
| UploadError::UnsupportedProcessExtension
|
2023-12-04 21:36:23 +00:00
|
|
|
| UploadError::SilentVideoDisabled
|
|
|
|
| UploadError::AggregateTimeout,
|
2022-03-29 01:47:46 +00:00
|
|
|
) => StatusCode::BAD_REQUEST,
|
2023-07-10 20:29:41 +00:00
|
|
|
Some(UploadError::Magick(e)) if e.is_client_error() => StatusCode::BAD_REQUEST,
|
|
|
|
Some(UploadError::Ffmpeg(e)) if e.is_client_error() => StatusCode::BAD_REQUEST,
|
|
|
|
Some(UploadError::Exiftool(e)) if e.is_client_error() => StatusCode::BAD_REQUEST,
|
2023-07-07 18:17:26 +00:00
|
|
|
Some(UploadError::MissingAlias) => StatusCode::NOT_FOUND,
|
2023-07-10 20:29:41 +00:00
|
|
|
Some(UploadError::Magick(e)) if e.is_not_found() => StatusCode::NOT_FOUND,
|
|
|
|
Some(UploadError::Ffmpeg(e)) if e.is_not_found() => StatusCode::NOT_FOUND,
|
2022-03-29 01:47:46 +00:00
|
|
|
Some(UploadError::InvalidToken) => StatusCode::FORBIDDEN,
|
|
|
|
Some(UploadError::Range) => StatusCode::RANGE_NOT_SATISFIABLE,
|
2020-06-06 21:41:17 +00:00
|
|
|
_ => StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-19 19:39:41 +00:00
|
|
|
fn error_response(&self) -> HttpResponse {
|
2023-07-16 17:21:46 +00:00
|
|
|
HttpResponse::build(self.status_code())
|
|
|
|
.content_type("application/json")
|
|
|
|
.body(
|
|
|
|
serde_json::to_string(&serde_json::json!({ "msg": self.root_cause().to_string() }))
|
|
|
|
.unwrap_or_else(|_| r#"{"msg":"Request failed"}"#.to_string()),
|
|
|
|
)
|
2020-06-06 21:41:17 +00:00
|
|
|
}
|
|
|
|
}
|