From 76619ea61e1eff60a7151513eb1efcb4a9613933 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sun, 13 Jul 2025 19:54:04 -0400 Subject: [PATCH] Added error checking for file creation in create metadata endpoint --- src/callers/song.rs | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/callers/song.rs b/src/callers/song.rs index d957ba9..0d53fd0 100644 --- a/src/callers/song.rs +++ b/src/callers/song.rs @@ -784,26 +784,34 @@ pub mod endpoint { let dir = std::path::Path::new(&song.directory); let save_path = dir.join(&song.filename); - let mut file = std::fs::File::create(&save_path).unwrap(); - file.write_all(&song.data).unwrap(); + // let mut file = std::fs::File::create(&save_path).unwrap(); + match std::fs::File::create(&save_path) { + Ok(mut file) => { + file.write_all(&song.data).unwrap(); - match song.song_path() { - Ok(_) => match super::song_db::insert(&pool, &song).await { - Ok((date_created, id)) => { - song.id = id; - song.date_created = date_created; - response.data.push(song); + match song.song_path() { + Ok(_) => match super::song_db::insert(&pool, &song).await { + Ok((date_created, id)) => { + song.id = id; + song.date_created = date_created; + response.data.push(song); - (axum::http::StatusCode::OK, axum::Json(response)) + (axum::http::StatusCode::OK, axum::Json(response)) + } + Err(err) => { + response.message = format!("{:?} song {:?}", err.to_string(), song); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } + }, + Err(err) => { + response.message = err.to_string(); + (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + } } - Err(err) => { - response.message = format!("{:?} song {:?}", err.to_string(), song); - (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) - } - }, + } Err(err) => { response.message = err.to_string(); - (axum::http::StatusCode::BAD_REQUEST, axum::Json(response)) + (axum::http::StatusCode::INTERNAL_SERVER_ERROR, axum::Json(response)) } } }