add error logging for internal server errors

master
neri 2 years ago
parent f97b3d79be
commit f2dfca2b39
  1. 16
      src/download.rs
  2. 21
      src/multipart.rs
  3. 9
      src/upload.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
}

@ -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)
}

@ -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",
)

Loading…
Cancel
Save