From f2dfca2b39d0123c7feccba96e3b8e3fc4ac73a2 Mon Sep 17 00:00:00 2001 From: neri Date: Thu, 8 Apr 2021 00:33:22 +0200 Subject: [PATCH] add error logging for internal server errors --- src/download.rs | 16 ++++++++++++---- src/multipart.rs | 21 ++++++++++++--------- src/upload.rs | 9 ++++++--- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/download.rs b/src/download.rs index 0cdee50..0cb7690 100644 --- a/src/download.rs +++ b/src/download.rs @@ -31,7 +31,10 @@ pub async fn download( let row: PgRow = rows .try_next() .await - .map_err(|_| error::ErrorInternalServerError("could not run select statement"))? + .map_err(|db_err| { + log::error!("could not run select statement {:?}", db_err); + error::ErrorInternalServerError("could not run select statement") + })? .ok_or_else(|| error::ErrorNotFound("file does not exist or has expired"))?; let file_id: String = row.get("file_id"); @@ -45,7 +48,8 @@ pub async fn download( let (content_type, mut content_disposition) = get_content_types(&path, &file_name); let is_text = file_kind == FileKind::Text.to_string() || content_type.type_() == mime::TEXT; let response = if is_text && !download { - let content = fs::read_to_string(path).await.map_err(|_| { + let content = fs::read_to_string(path).await.map_err(|file_err| { + log::error!("file could not be read {:?}", file_err); error::ErrorInternalServerError("this file should be here but could not be found") })?; let encoded = htmlescape::encode_minimal(&content); @@ -57,7 +61,8 @@ pub async fn download( content_disposition.disposition = DispositionType::Attachment; } let file = NamedFile::open(path) - .map_err(|_| { + .map_err(|file_err| { + log::error!("file could not be read {:?}", file_err); error::ErrorInternalServerError("this file should be here but could not be found") })? .set_content_type(content_type) @@ -67,7 +72,10 @@ pub async fn download( if delete_on_download { deleter::delete_by_id(&db, &file_id, &config.files_dir) .await - .map_err(|_| error::ErrorInternalServerError("could not delete file"))?; + .map_err(|db_err| { + log::error!("could not delete file {:?}", db_err); + error::ErrorInternalServerError("could not delete file") + })?; } response } diff --git a/src/multipart.rs b/src/multipart.rs index b30f527..a823c07 100644 --- a/src/multipart.rs +++ b/src/multipart.rs @@ -39,9 +39,10 @@ pub(crate) async fn parse_multipart( } original_name = file_original_name; kind = Some(FileKind::Binary); - let mut file = fs::File::create(&filename) - .await - .map_err(|_| error::ErrorInternalServerError("could not create file"))?; + let mut file = fs::File::create(&filename).await.map_err(|file_err| { + log::error!("could not create file {:?}", file_err); + error::ErrorInternalServerError("could not create file") + })?; size = write_to_file(&mut file, field, config.max_file_size).await?; } "text" => { @@ -50,9 +51,10 @@ pub(crate) async fn parse_multipart( } original_name = Some(format!("{}.txt", file_id)); kind = Some(FileKind::Text); - let mut file = fs::File::create(&filename) - .await - .map_err(|_| error::ErrorInternalServerError("could not create file"))?; + let mut file = fs::File::create(&filename).await.map_err(|file_err| { + log::error!("could not create file {:?}", file_err); + error::ErrorInternalServerError("could not create file") + })?; size = write_to_file(&mut file, field, config.max_file_size).await?; } "delete_on_download" => { @@ -157,9 +159,10 @@ async fn write_to_file( ))); } } - file.write_all(chunk.as_ref()) - .await - .map_err(|_| error::ErrorInternalServerError("could not write file"))?; + file.write_all(chunk.as_ref()).await.map_err(|write_err| { + log::error!("could not write file {:?}", write_err); + error::ErrorInternalServerError("could not write file") + })?; } Ok(written_bytes) } diff --git a/src/upload.rs b/src/upload.rs index f4bdf45..75bfc08 100644 --- a/src/upload.rs +++ b/src/upload.rs @@ -75,7 +75,8 @@ pub async fn upload( Ok(data) => data, Err(err) => { if filename.exists().await { - fs::remove_file(filename).await.map_err(|_| { + fs::remove_file(filename).await.map_err(|file_err| { + log::error!("could not remove file {:?}", file_err); error::ErrorInternalServerError( "could not parse multipart; could not remove file", ) @@ -96,8 +97,10 @@ pub async fn upload( .bind(delete_on_download) .execute(db.as_ref()) .await; - if db_insert.is_err() { - fs::remove_file(filename).await.map_err(|_| { + if let Err(db_err) = db_insert { + log::error!("could not insert into datebase {:?}", db_err); + fs::remove_file(filename).await.map_err(|file_err| { + log::error!("could not remove file {:?}", file_err); error::ErrorInternalServerError( "could not insert file into database; could not remove file", )