2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-22 19:31:35 +00:00

Port error changes to 0.5

This commit is contained in:
asonix 2023-07-16 21:51:14 -05:00
parent 2cd7140874
commit a751d92436
3 changed files with 18 additions and 17 deletions

View file

@ -9,6 +9,10 @@ impl Error {
fn kind(&self) -> Option<&UploadError> { fn kind(&self) -> Option<&UploadError> {
self.inner.downcast_ref() self.inner.downcast_ref()
} }
pub(crate) fn root_cause(&self) -> &(dyn std::error::Error + 'static) {
self.inner.root_cause()
}
} }
impl std::fmt::Debug for Error { impl std::fmt::Debug for Error {
@ -188,20 +192,11 @@ impl ResponseError for Error {
} }
fn error_response(&self) -> HttpResponse { fn error_response(&self) -> HttpResponse {
if let Some(kind) = self.kind() {
HttpResponse::build(self.status_code()) HttpResponse::build(self.status_code())
.content_type("application/json") .content_type("application/json")
.body( .body(
serde_json::to_string(&serde_json::json!({ "msg": kind.to_string() })) serde_json::to_string(&serde_json::json!({ "msg": self.root_cause().to_string() }))
.unwrap_or_else(|_| r#"{"msg":"Request failed"}"#.to_string()),
)
} else {
HttpResponse::build(self.status_code())
.content_type("application/json")
.body(
serde_json::to_string(&serde_json::json!({ "msg": "Unknown error" }))
.unwrap_or_else(|_| r#"{"msg":"Request failed"}"#.to_string()), .unwrap_or_else(|_| r#"{"msg":"Request failed"}"#.to_string()),
) )
} }
} }
}

View file

@ -44,9 +44,12 @@ pin_project_lite::pin_project! {
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub(crate) enum ProcessError { pub(crate) enum ProcessError {
#[error("Required command {0} not found")] #[error("Required command {0} not found, make sure it exists in pict-rs' $PATH")]
NotFound(String), NotFound(String),
#[error("Cannot run command {0} due to invalid permissions on binary, make sure the pict-rs user has permission to run it")]
PermissionDenied(String),
#[error("Reached process spawn limit")] #[error("Reached process spawn limit")]
LimitReached, LimitReached,
@ -66,6 +69,9 @@ impl Process {
Ok(this) => Ok(this), Ok(this) => Ok(this),
Err(e) => match e.kind() { Err(e) => match e.kind() {
std::io::ErrorKind::NotFound => Err(ProcessError::NotFound(command.to_string())), std::io::ErrorKind::NotFound => Err(ProcessError::NotFound(command.to_string())),
std::io::ErrorKind::PermissionDenied => {
Err(ProcessError::PermissionDenied(command.to_string()))
}
std::io::ErrorKind::WouldBlock => Err(ProcessError::LimitReached), std::io::ErrorKind::WouldBlock => Err(ProcessError::LimitReached),
_ => Err(ProcessError::Other(e)), _ => Err(ProcessError::Other(e)),
}, },

View file

@ -105,7 +105,7 @@ where
tracing::warn!("Failed to ingest\n{}\n{}", format!("{e}"), format!("{e:?}")); tracing::warn!("Failed to ingest\n{}\n{}", format!("{e}"), format!("{e:?}"));
UploadResult::Failure { UploadResult::Failure {
message: e.to_string(), message: e.root_cause().to_string(),
} }
} }
}; };