on master help sign in
Improve errors handling in main by adding context
ea824edDanilo Spinella
helix-term/src/main.rs+11−12
@@ -10,9 +10,9 @@ use application::Application;
10
10
11use std::path::PathBuf;
11use std::path::PathBuf;
12
12
13−use anyhow::Error;
13+use anyhow::{Context, Result};
14
14
15−fn setup_logging(verbosity: u64) -> Result<(), fern::InitError> {
15+fn setup_logging(verbosity: u64) -> Result<()> {
16 let mut base_config = fern::Dispatch::new();
16 let mut base_config = fern::Dispatch::new();
17
17
18 // Let's say we depend on something which whose "info" level messages are too
18 // Let's say we depend on something which whose "info" level messages are too
@@ -27,7 +27,7 @@ fn setup_logging(verbosity: u64) -> Result<(), fern::InitError> {
27 _3_or_more => base_config.level(log::LevelFilter::Trace),
27 _3_or_more => base_config.level(log::LevelFilter::Trace),
28 };
28 };
29
29
30− let home = dirs_next::home_dir().expect("can't find the home directory");
30+ let home = dirs_next::home_dir().context("can't find the home directory")?;
31
31
32 // Separate file config so we can include year, month and day in file logs
32 // Separate file config so we can include year, month and day in file logs
33 let file_config = fern::Dispatch::new()
33 let file_config = fern::Dispatch::new()
@@ -51,7 +51,7 @@ pub struct Args {
51 files: Vec<PathBuf>,
51 files: Vec<PathBuf>,
52}
52}
53
53
54−fn main() {
54+fn main() -> Result<()> {
55 let help = format!(
55 let help = format!(
56 "\
56 "\
57{} {}
57{} {}
@@ -89,7 +89,7 @@ FLAGS:
89 verbosity = 1;
89 verbosity = 1;
90 }
90 }
91
91
92− setup_logging(verbosity).expect("failed to initialize logging.");
92+ setup_logging(verbosity).context("failed to initialize logging")?;
93
93
94 let args = Args {
94 let args = Args {
95 files: pargs.finish().into_iter().map(|arg| arg.into()).collect(),
95 files: pargs.finish().into_iter().map(|arg| arg.into()).collect(),
@@ -105,17 +105,16 @@ FLAGS:
105 .as_deref()
105 .as_deref()
106 .unwrap_or(include_bytes!("../../languages.toml"));
106 .unwrap_or(include_bytes!("../../languages.toml"));
107
107
108− LOADER.get_or_init(|| {
109− let config = toml::from_slice(toml).expect("Could not parse languages.toml");
108+ let config = toml::from_slice(toml).context("Could not parse languages.toml")?;
110− Loader::new(config)
109+ LOADER.get_or_init(|| Loader::new(config));
111− });
112
110
113− let runtime = tokio::runtime::Runtime::new().unwrap();
111+ let runtime = tokio::runtime::Runtime::new().context("unable to start tokio runtime")?;
114
112
115 // TODO: use the thread local executor to spawn the application task separately from the work pool
113 // TODO: use the thread local executor to spawn the application task separately from the work pool
114+ let mut app = Application::new(args).context("unable to create new appliction")?;
116 runtime.block_on(async move {
115 runtime.block_on(async move {
117− let mut app = Application::new(args).unwrap();
118−
119 app.run().await;
116 app.run().await;
120 });
117 });
118+
119+ Ok(())
121}
120}