From fe1c3ebcdf7367f7e77cd2e93e29046b6c97e3a4 Mon Sep 17 00:00:00 2001 From: "Gustavo \"Guz\" L. de Mello" Date: Wed, 17 Apr 2024 18:22:08 -0300 Subject: [PATCH] fix: error handling on input reading --- src/main.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index aee63a9..63e0f89 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,7 +76,23 @@ enum FrontmatterCommands { fn main() { let mut cli = Cli::parse(); - let file = std::io::read_to_string(&mut cli.input).unwrap_or_else(|err| panic!("{err:#?}")); + let file = match std::io::read_to_string(&mut cli.input) { + Ok(s) => s, + Err(e) => { + cli::print_error( + cli::Error { + code: cli::ErrorCode::EIORD, + description: format!("Failed to read input\n{e:#?}"), + fix: Some(String::from( + "Try to check if you have input permission to input file", + )), + url: None, + }, + cli.surpress_errors, + ); + return (); + } + }; let arena = comrak::Arena::new(); let ast = comrak::parse_document(&arena, &file, &mdparser::utils::default_options()); @@ -177,6 +193,7 @@ mod cli { #[derive(Debug)] pub enum ErrorCode { EPRSG, + EIORD, EIOWR, } impl fmt::Display for ErrorCode { @@ -196,6 +213,7 @@ mod cli { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let title = match self.code { ErrorCode::EPRSG => "Parsing error", + ErrorCode::EIORD => "IO error on read operation", ErrorCode::EIOWR => "IO error on write operation", };