2
0
mirror of https://github.com/sharkdp/bat synced 2024-11-18 15:26:16 +00:00

Better error handling

This commit is contained in:
sharkdp 2018-05-16 22:23:53 +02:00 committed by David Peter
parent 9aa97b6c22
commit c826c2a438

View File

@ -26,16 +26,8 @@ impl HighlightingAssets {
let source_dir = dir.unwrap_or_else(|| PROJECT_DIRS.config_dir()); let source_dir = dir.unwrap_or_else(|| PROJECT_DIRS.config_dir());
let theme_dir = source_dir.join("themes"); let theme_dir = source_dir.join("themes");
let theme_set = ThemeSet::load_from_folder(&theme_dir).map_err(|_| { let theme_set =
io::Error::new( ThemeSet::load_from_folder(&theme_dir).chain_err(|| "Could not load themes from '{}'")?;
io::ErrorKind::Other,
format!(
"Could not load themes from '{}'",
theme_dir.to_string_lossy()
),
)
})?;
let mut syntax_set = SyntaxSet::new(); let mut syntax_set = SyntaxSet::new();
let syntax_dir = source_dir.join("syntaxes"); let syntax_dir = source_dir.join("syntaxes");
if !syntax_dir.exists() { if !syntax_dir.exists() {
@ -61,9 +53,8 @@ impl HighlightingAssets {
syntax_set_path().to_string_lossy() syntax_set_path().to_string_lossy()
) )
})?; })?;
let mut syntax_set: SyntaxSet = from_reader(syntax_set_file).map_err(|_| { let mut syntax_set: SyntaxSet =
io::Error::new(io::ErrorKind::Other, "Could not parse cached syntax set") from_reader(syntax_set_file).chain_err(|| "Could not parse cached syntax set")?;
})?;
syntax_set.link_syntaxes(); syntax_set.link_syntaxes();
let theme_set_file = File::open(&theme_set_path).chain_err(|| { let theme_set_file = File::open(&theme_set_path).chain_err(|| {
@ -72,8 +63,8 @@ impl HighlightingAssets {
theme_set_path.to_string_lossy() theme_set_path.to_string_lossy()
) )
})?; })?;
let theme_set: ThemeSet = from_reader(theme_set_file) let theme_set: ThemeSet =
.map_err(|_| io::Error::new(io::ErrorKind::Other, "Could not parse cached theme set"))?; from_reader(theme_set_file).chain_err(|| "Could not parse cached theme set")?;
Ok(HighlightingAssets { Ok(HighlightingAssets {
syntax_set, syntax_set,
@ -102,13 +93,10 @@ impl HighlightingAssets {
"Writing theme set to {} ... ", "Writing theme set to {} ... ",
theme_set_path.to_string_lossy() theme_set_path.to_string_lossy()
); );
dump_to_file(&self.theme_set, &theme_set_path).map_err(|_| { dump_to_file(&self.theme_set, &theme_set_path).chain_err(|| {
io::Error::new( format!(
io::ErrorKind::Other, "Could not save theme set to {}",
format!( theme_set_path.to_string_lossy()
"Could not save theme set to {}",
theme_set_path.to_string_lossy()
),
) )
})?; })?;
println!("okay"); println!("okay");
@ -117,13 +105,10 @@ impl HighlightingAssets {
"Writing syntax set to {} ... ", "Writing syntax set to {} ... ",
syntax_set_path.to_string_lossy() syntax_set_path.to_string_lossy()
); );
dump_to_file(&self.syntax_set, &syntax_set_path).map_err(|_| { dump_to_file(&self.syntax_set, &syntax_set_path).chain_err(|| {
io::Error::new( format!(
io::ErrorKind::Other, "Could not save syntax set to {}",
format!( syntax_set_path.to_string_lossy()
"Could not save syntax set to {}",
syntax_set_path.to_string_lossy()
),
) )
})?; })?;
println!("okay"); println!("okay");