From 8ecc2943a4241d466a9bf5a359260a958e3d3b46 Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Tue, 26 May 2020 22:01:49 -0400 Subject: cleaner error handling and verbose logging in main() --- src/main.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 53cb2f9..8b6cb9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,11 +11,10 @@ mod logging; mod posts; mod user; -fn main() -> error::Result<()> { +fn main() { let arg_matches = &*conf::ARGS; let start = time::Instant::now(); - let logfile = format!("/tmp/clinte_{}.log", *user::NAME); - logging::init(&logfile)?; + logging::checked_init(); log::info!("clinte starting up!"); println!("clinte v{}", clap::crate_version!()); @@ -30,26 +29,63 @@ fn main() -> error::Result<()> { if arg_matches.subcommand_matches("post").is_some() { log::info!("New post..."); - posts::create(&db)?; + if let Err(e) = posts::create(&db) { + log::error!("Error creating new post"); + if *conf::DEBUG { + log::error!("--> {}", e); + } + std::process::exit(1); + } } else if arg_matches.subcommand_matches("update").is_some() { let id: u32 = if let Some(val) = arg_matches.subcommand_matches("update_handler") { - val.value_of("id").unwrap().parse()? + 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); + } + } } else { 0 }; log::info!("Updating post ..."); - posts::update_handler(&db, id)?; + 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); + } } else if arg_matches.subcommand_matches("delete").is_some() { let id: u32 = if let Some(val) = arg_matches.subcommand_matches("update_handler") { - val.value_of("id").unwrap_or_else(|| "0").parse()? + match val.value_of("id").unwrap_or_else(|| "0").parse() { + Ok(n) => n, + Err(_) => { + log::error!("Couldn't parse ID"); + std::process::exit(1); + } + } } else { 0 }; log::info!("Deleting post"); - posts::delete_handler(&db, id)?; + 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); + } } - posts::display(&db)?; - - Ok(()) + if let Err(e) = posts::display(&db) { + log::error!("Error displaying posts"); + if *conf::DEBUG { + log::error!("--> {}", e); + } + std::process::exit(1); + } } -- cgit 1.4.1-2-gfad0 '>d339004f ^
f8de2823 ^

b3be26a2 ^
d339004f ^
f8de2823 ^














f9737251 ^











f8de2823 ^




f3881db1 ^

5e7d2f5f ^


965e82b3 ^
5e7d2f5f ^
965e82b3 ^
5e7d2f5f ^

d339004f ^

894360dc ^
965e82b3 ^
5e7d2f5f ^
13f0166a ^

76bd2ec1 ^
13f0166a ^




5e7d2f5f ^
5ce97728 ^
f8de2823 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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