2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2025-01-11 20:15:49 +00:00

Return Root Cause error from backgrounded endpoint

This commit is contained in:
asonix 2023-07-16 12:21:46 -05:00
parent f9b8e817e6
commit ee0ce5bc38
2 changed files with 11 additions and 16 deletions

View file

@ -9,6 +9,10 @@ impl Error {
fn kind(&self) -> Option<&UploadError> {
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 {
@ -185,20 +189,11 @@ impl ResponseError for Error {
}
fn error_response(&self) -> HttpResponse {
if let Some(kind) = self.kind() {
HttpResponse::build(self.status_code())
.content_type("application/json")
.body(
serde_json::to_string(&serde_json::json!({ "msg": kind.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()),
)
}
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()),
)
}
}

View file

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