error handling
This commit is contained in:
@@ -47,6 +47,7 @@ use crate::schema::{
|
||||
use crate::state::AppState;
|
||||
use crate::utils::{
|
||||
db::{no_content, validate_bulk_ids, IntoJsonResponse},
|
||||
error::StorageResultExt,
|
||||
http::inline_content_disposition,
|
||||
json::{classify_nullable, NullableValue},
|
||||
storage_paths::document_version_object_key,
|
||||
@@ -412,7 +413,10 @@ pub async fn list_documents(
|
||||
|
||||
let ids = quickwit_search(endpoint, index, tenant_id, query_str)
|
||||
.await
|
||||
.map_err(|err| AppError::internal(format!("quickwit search failed: {err}")))?;
|
||||
.map_err(|err| {
|
||||
error!(error = ?err, "quickwit search failed");
|
||||
AppError::internal("quickwit search failed")
|
||||
})?;
|
||||
|
||||
if ids.is_empty() {
|
||||
return Ok(Json(vec![]));
|
||||
@@ -913,7 +917,10 @@ pub async fn request_document_assets(
|
||||
}),
|
||||
None,
|
||||
)
|
||||
.map_err(|err| AppError::internal(format!("failed to enqueue analyze job: {err}")))?;
|
||||
.map_err(|err| {
|
||||
error!(error = ?err, "failed to enqueue analyze job");
|
||||
AppError::internal("failed to enqueue analyze job")
|
||||
})?;
|
||||
|
||||
Ok(StatusCode::ACCEPTED)
|
||||
}
|
||||
@@ -959,7 +966,10 @@ pub async fn reanalyze_selected_documents(
|
||||
}),
|
||||
None,
|
||||
)
|
||||
.map_err(|err| AppError::internal(format!("failed to enqueue analyze job: {err}")))?;
|
||||
.map_err(|err| {
|
||||
error!(error = ?err, "failed to enqueue analyze job");
|
||||
AppError::internal("failed to enqueue analyze job")
|
||||
})?;
|
||||
queued += 1;
|
||||
}
|
||||
|
||||
@@ -1048,7 +1058,7 @@ pub async fn get_document_asset(
|
||||
Duration::from_secs(PRESIGNED_URL_EXPIRY_SECONDS),
|
||||
)
|
||||
.await
|
||||
.map_err(|err| AppError::internal(format!("failed to generate asset URL: {err}")))?;
|
||||
.storage_context("failed to generate asset URL")?;
|
||||
|
||||
object_responses.push(to_asset_object_response(
|
||||
object,
|
||||
@@ -1178,7 +1188,7 @@ pub async fn download_with_token(
|
||||
Duration::from_secs(PRESIGNED_URL_EXPIRY_SECONDS),
|
||||
)
|
||||
.await
|
||||
.map_err(|err| AppError::internal(format!("failed to generate download URL: {err}")))?;
|
||||
.storage_context("failed to generate download URL")?;
|
||||
|
||||
Ok(axum::response::Redirect::temporary(&presigned_url))
|
||||
}
|
||||
@@ -2035,10 +2045,7 @@ async fn process_upload(
|
||||
content_disposition.clone(),
|
||||
)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
error!(error = %err, key = %s3_key, "failed to store document");
|
||||
AppError::internal(format!("failed to store document: {err}"))
|
||||
})?;
|
||||
.storage_context("failed to store document")?;
|
||||
|
||||
let metadata_value = if metadata.is_null() {
|
||||
Value::Object(Default::default())
|
||||
|
||||
@@ -164,7 +164,7 @@ pub async fn ensure_folder_path(
|
||||
last_folder = Some(folder);
|
||||
}
|
||||
|
||||
last_folder.ok_or_else(|| AppError::internal("failed to resolve folder path".to_string()))
|
||||
last_folder.ok_or_else(|| AppError::internal("failed to resolve folder path"))
|
||||
})?;
|
||||
|
||||
Ok(Json(FolderResponse {
|
||||
|
||||
@@ -25,7 +25,7 @@ use crate::schema::{
|
||||
user_memberships::dsl as memberships_dsl, users::dsl as users_dsl,
|
||||
};
|
||||
use crate::state::AppState;
|
||||
use crate::utils::{http::inline_content_disposition, time::to_http_date};
|
||||
use crate::utils::{error::StorageResultExt, http::inline_content_disposition, time::to_http_date};
|
||||
|
||||
const REALM: &str = "Papercrate WebDAV";
|
||||
const DOWNLOAD_URL_TTL_SECONDS: u64 = 300;
|
||||
@@ -108,8 +108,10 @@ async fn handle_propfind(
|
||||
}
|
||||
};
|
||||
|
||||
let body = render_multistatus(&resources)
|
||||
.map_err(|err| AppError::internal(format!("failed to render WebDAV response: {err}")))?;
|
||||
let body = render_multistatus(&resources).map_err(|err| {
|
||||
tracing::error!(error = ?err, "failed to render WebDAV response");
|
||||
AppError::internal("failed to render WebDAV response")
|
||||
})?;
|
||||
|
||||
let response = Response::builder()
|
||||
.status(multi_status())
|
||||
@@ -320,7 +322,7 @@ async fn stream_document(
|
||||
Duration::from_secs(DOWNLOAD_URL_TTL_SECONDS),
|
||||
)
|
||||
.await
|
||||
.map_err(|err| AppError::internal(format!("failed to presign document download: {err}")))?;
|
||||
.storage_context("failed to presign document download")?;
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let mut request = client.request(method.clone(), url.clone());
|
||||
@@ -329,18 +331,17 @@ async fn stream_document(
|
||||
request = request.header(header::RANGE, range.clone());
|
||||
}
|
||||
|
||||
let upstream = request
|
||||
.send()
|
||||
.await
|
||||
.map_err(|err| AppError::internal(format!("failed to fetch document stream: {err}")))?;
|
||||
let upstream = request.send().await.map_err(|err| {
|
||||
tracing::error!(error = ?err, "failed to fetch document stream");
|
||||
AppError::internal("failed to fetch document stream")
|
||||
})?;
|
||||
|
||||
let status =
|
||||
StatusCode::from_u16(upstream.status().as_u16()).unwrap_or(StatusCode::BAD_GATEWAY);
|
||||
|
||||
if !(status.is_success() || status == StatusCode::PARTIAL_CONTENT) {
|
||||
return Err(AppError::internal(format!(
|
||||
"upstream download returned status {status}"
|
||||
)));
|
||||
tracing::error!(status = %status, "upstream download returned error status");
|
||||
return Err(AppError::internal("failed to fetch document stream"));
|
||||
}
|
||||
|
||||
let mut builder = Response::builder().status(status);
|
||||
@@ -368,9 +369,10 @@ async fn stream_document(
|
||||
builder = builder.header(header::ETAG, format!("\"{}\"", version.id));
|
||||
|
||||
if method == Method::HEAD {
|
||||
return builder
|
||||
.body(Body::empty())
|
||||
.map_err(|err| AppError::internal(format!("failed to build response: {err}")));
|
||||
return builder.body(Body::empty()).map_err(|err| {
|
||||
tracing::error!(error = ?err, "failed to build WebDAV response");
|
||||
AppError::internal("failed to build WebDAV response")
|
||||
});
|
||||
}
|
||||
|
||||
let stream = upstream
|
||||
@@ -378,9 +380,10 @@ async fn stream_document(
|
||||
.map(|chunk| chunk.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err)));
|
||||
let body = Body::from_stream(stream);
|
||||
|
||||
builder
|
||||
.body(body)
|
||||
.map_err(|err| AppError::internal(format!("failed to build response: {err}")))
|
||||
builder.body(body).map_err(|err| {
|
||||
tracing::error!(error = ?err, "failed to build WebDAV response");
|
||||
AppError::internal("failed to build WebDAV response")
|
||||
})
|
||||
}
|
||||
|
||||
fn authenticate(state: &AppState, headers: &HeaderMap) -> Result<Option<WebDavContext>, AppError> {
|
||||
|
||||
Reference in New Issue
Block a user