add error logging for internal server errors

This commit is contained in:
neri 2021-04-08 00:33:22 +02:00
parent 229e428de3
commit 74d06f53a1
3 changed files with 30 additions and 16 deletions

View File

@ -31,7 +31,10 @@ pub async fn download(
let row: PgRow = rows let row: PgRow = rows
.try_next() .try_next()
.await .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"))?; .ok_or_else(|| error::ErrorNotFound("file does not exist or has expired"))?;
let file_id: String = row.get("file_id"); 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 (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 is_text = file_kind == FileKind::Text.to_string() || content_type.type_() == mime::TEXT;
let response = if is_text && !download { 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") error::ErrorInternalServerError("this file should be here but could not be found")
})?; })?;
let encoded = htmlescape::encode_minimal(&content); let encoded = htmlescape::encode_minimal(&content);
@ -57,7 +61,8 @@ pub async fn download(
content_disposition.disposition = DispositionType::Attachment; content_disposition.disposition = DispositionType::Attachment;
} }
let file = NamedFile::open(path) 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") error::ErrorInternalServerError("this file should be here but could not be found")
})? })?
.set_content_type(content_type) .set_content_type(content_type)
@ -67,7 +72,10 @@ pub async fn download(
if delete_on_download { if delete_on_download {
deleter::delete_by_id(&db, &file_id, &config.files_dir) deleter::delete_by_id(&db, &file_id, &config.files_dir)
.await .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 response
} }

View File

@ -39,9 +39,10 @@ pub(crate) async fn parse_multipart(
} }
original_name = file_original_name; original_name = file_original_name;
kind = Some(FileKind::Binary); kind = Some(FileKind::Binary);
let mut file = fs::File::create(&filename) let mut file = fs::File::create(&filename).await.map_err(|file_err| {
.await log::error!("could not create file {:?}", file_err);
.map_err(|_| error::ErrorInternalServerError("could not create file"))?; error::ErrorInternalServerError("could not create file")
})?;
size = write_to_file(&mut file, field, config.max_file_size).await?; size = write_to_file(&mut file, field, config.max_file_size).await?;
} }
"text" => { "text" => {
@ -50,9 +51,10 @@ pub(crate) async fn parse_multipart(
} }
original_name = Some(format!("{}.txt", file_id)); original_name = Some(format!("{}.txt", file_id));
kind = Some(FileKind::Text); kind = Some(FileKind::Text);
let mut file = fs::File::create(&filename) let mut file = fs::File::create(&filename).await.map_err(|file_err| {
.await log::error!("could not create file {:?}", file_err);
.map_err(|_| error::ErrorInternalServerError("could not create file"))?; error::ErrorInternalServerError("could not create file")
})?;
size = write_to_file(&mut file, field, config.max_file_size).await?; size = write_to_file(&mut file, field, config.max_file_size).await?;
} }
"delete_on_download" => { "delete_on_download" => {
@ -157,9 +159,10 @@ async fn write_to_file(
))); )));
} }
} }
file.write_all(chunk.as_ref()) file.write_all(chunk.as_ref()).await.map_err(|write_err| {
.await log::error!("could not write file {:?}", write_err);
.map_err(|_| error::ErrorInternalServerError("could not write file"))?; error::ErrorInternalServerError("could not write file")
})?;
} }
Ok(written_bytes) Ok(written_bytes)
} }

View File

@ -75,7 +75,8 @@ pub async fn upload(
Ok(data) => data, Ok(data) => data,
Err(err) => { Err(err) => {
if filename.exists().await { 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( error::ErrorInternalServerError(
"could not parse multipart; could not remove file", "could not parse multipart; could not remove file",
) )
@ -96,8 +97,10 @@ pub async fn upload(
.bind(delete_on_download) .bind(delete_on_download)
.execute(db.as_ref()) .execute(db.as_ref())
.await; .await;
if db_insert.is_err() { if let Err(db_err) = db_insert {
fs::remove_file(filename).await.map_err(|_| { 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( error::ErrorInternalServerError(
"could not insert file into database; could not remove file", "could not insert file into database; could not remove file",
) )