From e489f7d9f8b799ee1605b4d5cb65fe7adf24ea22 Mon Sep 17 00:00:00 2001 From: Qrius Date: Thu, 26 Sep 2024 00:11:05 +0200 Subject: Add more error handling --- src/skaldpress/main.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/skaldpress/main.rs b/src/skaldpress/main.rs index 265f7fc..79ea7ab 100644 --- a/src/skaldpress/main.rs +++ b/src/skaldpress/main.rs @@ -10,7 +10,9 @@ const CONTENT_DIR: &str = "content/"; const BUILD_DIR: &str = "build/"; fn compile_file(file_path: &Path) -> Result { - let extension = file_path.extension().expect("SP14"); + let extension = file_path + .extension() + .ok_or(SkaldpressError::PathOperationError(7, None))?; let file_content = fs::read_to_string(file_path).map_err(|e| { SkaldpressError::FileReadError( @@ -74,7 +76,7 @@ fn compile_file_and_write(source_file_path: &Path) -> Result<(), Box Result<(), SkaldpressError> { for entry in fs::read_dir(directory).map_err(|e| { SkaldpressError::DirectoryReadError( - 6, + 8, e, directory.to_str().unwrap_or("unknown dir").to_string(), ) @@ -87,7 +89,13 @@ fn compile_files_in_directory(directory: &Path) -> Result<(), SkaldpressError> { } }; let path = entry.path(); - let metadata = fs::metadata(&path).expect("SP6"); + let metadata = match fs::metadata(&path) { + Ok(metadata) => metadata, + Err(e) => { + println!("\x1b[31mError getting file metadata {:#?}\x1b[0m", e); + continue; + } + }; if metadata.is_file() { println!("Compiling {:#?}", path.as_path()); @@ -118,5 +126,5 @@ fn main() { let _ = compile_files_in_directory(Path::new(CONTENT_DIR)); // Just for testing - //compile_file_and_write(Path::new("content/test.html")).expect("AYYYYO"); + //compile_file_and_write(Path::new("content/test.html")); } -- cgit v1.2.3