From 841e6a34a49261d2681c8a4d2c1dc176ba4e3b35 Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Tue, 26 May 2020 23:15:51 -0400 Subject: removed panics and refactored error handling Using a helper function to handle fatal errors error::helper() Displays the simplified message if an error condition occurs. Displays both the simplified and the raw error message if -v verbose logging is enabled. --- src/main.rs | 67 ++++++++++++++++++++----------------------------------------- 1 file changed, 22 insertions(+), 45 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 8b6cb9a..06fb4ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,63 +29,40 @@ fn main() { if arg_matches.subcommand_matches("post").is_some() { log::info!("New post..."); - if let Err(e) = posts::create(&db) { - log::error!("Error creating new post"); - if *conf::DEBUG { - log::error!("--> {}", e); - } - std::process::exit(1); - } + error::helper(posts::create(&db), "Error creating new post"); } else if arg_matches.subcommand_matches("update").is_some() { let id: u32 = if let Some(val) = arg_matches.subcommand_matches("update_handler") { - match val.value_of("id").unwrap_or_else(|| "0").parse() { - Ok(n) => n, - Err(e) => { - log::error!("Couldn't parse ID"); - if *conf::DEBUG { - log::error!("--> {}", e); - } - std::process::exit(1); - } - } + error::helper( + val.value_of("id").unwrap_or_else(|| "0").parse(), + "Couldn't parse ID", + ) } else { 0 }; + log::info!("Updating post ..."); - if let Err(e) = posts::update_handler(&db, id) { - log::error!("Error updating post {}", id); - if *conf::DEBUG { - log::error!("--> {}", e); - } - std::process::exit(1); - } + + error::helper( + posts::update_handler(&db, id), + format!("Error updating post {}", id).as_ref(), + ); } else if arg_matches.subcommand_matches("delete").is_some() { let id: u32 = if let Some(val) = arg_matches.subcommand_matches("update_handler") { - match val.value_of("id").unwrap_or_else(|| "0").parse() { - Ok(n) => n, - Err(_) => { - log::error!("Couldn't parse ID"); - std::process::exit(1); - } - } + error::helper( + val.value_of("id").unwrap_or_else(|| "0").parse(), + "Couldn't parse ID", + ) } else { 0 }; + log::info!("Deleting post"); - if let Err(e) = posts::delete_handler(&db, id) { - log::error!("Error deleting post {}", id); - if *conf::DEBUG { - log::error!("--> {}", e); - } - std::process::exit(1); - } - } - if let Err(e) = posts::display(&db) { - log::error!("Error displaying posts"); - if *conf::DEBUG { - log::error!("--> {}", e); - } - std::process::exit(1); + error::helper( + posts::delete_handler(&db, id), + format!("Error deleting post {}", id).as_ref(), + ); } + + error::helper(posts::display(&db), "Error displaying posts"); } -- cgit 1.4.1-2-gfad0 2' href='#n32'>32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155