diff options
author | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2019-11-16 00:33:22 +0200 |
---|---|---|
committer | Manos Pitsidianakis <el13635@mail.ntua.gr> | 2019-11-16 00:33:22 +0200 |
commit | 321be8555fe194d2e39a9f07342186fb23fb53a1 (patch) | |
tree | 77a287679c4abef6564f02ffff49e67452660b57 /ui/src/conf.rs | |
parent | aeb9d046a27929158565c21399205b3f06767f41 (diff) |
Cleanup startup error exit paths
Make startup methods return Results so that the main binary can exit
cleanly instead of using std::process::exit from arbitrary positions,
which exits the process immediately and doesn't run destructors.
Diffstat (limited to 'ui/src/conf.rs')
-rw-r--r-- | ui/src/conf.rs | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/ui/src/conf.rs b/ui/src/conf.rs index 79c9b113..184c706e 100644 --- a/ui/src/conf.rs +++ b/ui/src/conf.rs @@ -312,10 +312,12 @@ impl FileSettings { file.write_all(include_bytes!("../../sample-config")) .expect("Could not write to config file."); println!("Written config to {}", config_path.display()); - std::process::exit(1); + return Err(MeliError::new( + "Edit the sample configuration and relaunch meli.", + )); } "n" | "N" | "no" | "No" | "NO" => { - std::process::exit(1); + return Err(MeliError::new("No configuration file found.")); } _ => { println!( @@ -332,8 +334,10 @@ impl FileSettings { file.read_to_string(&mut contents)?; let s = toml::from_str(&contents); if let Err(e) = s { - eprintln!("Config file contains errors: {}", e.to_string()); - std::process::exit(1); + return Err(MeliError::new(format!( + "Config file contains errors: {}", + e.to_string() + ))); } Ok(s.unwrap()) @@ -341,11 +345,8 @@ impl FileSettings { } impl Settings { - pub fn new() -> Settings { - let fs = FileSettings::new().unwrap_or_else(|e| { - eprintln!("Configuration error: {}", e); - std::process::exit(1); - }); + pub fn new() -> Result<Settings> { + let fs = FileSettings::new()?; let mut s: HashMap<String, AccountConf> = HashMap::new(); for (id, x) in fs.accounts { @@ -355,7 +356,7 @@ impl Settings { s.insert(id, ac); } - Settings { + Ok(Settings { accounts: s, pager: fs.pager, notifications: fs.notifications, @@ -363,7 +364,7 @@ impl Settings { composing: fs.composing, pgp: fs.pgp, terminal: fs.terminal, - } + }) } } |