bat/src/error.rs

53 lines
1.6 KiB
Rust
Raw Normal View History

2020-03-21 19:35:04 +01:00
use error_chain::error_chain;
use std::io::Write;
2020-03-21 19:35:04 +01:00
error_chain! {
foreign_links {
Clap(::clap::Error) #[cfg(feature = "minimal-application")];
2020-03-30 22:18:41 +02:00
Io(::std::io::Error);
SyntectError(::syntect::LoadingError);
ParseIntError(::std::num::ParseIntError);
GlobParsingError(::globset::Error);
SerdeYamlError(::serde_yaml::Error);
2020-03-21 19:35:04 +01:00
}
errors {
UndetectedSyntax(input: String) {
description("unable to detect syntax"),
display("unable to detect syntax for {}", input)
}
UnknownSyntax(name: String) {
description("unknown syntax"),
display("unknown syntax: '{}'", name)
}
InvalidPagerValueBat {
description("invalid value `bat` for pager property"),
display("Use of bat as a pager is disallowed in order to avoid infinite recursion problems")
}
}
2020-03-21 19:35:04 +01:00
}
pub fn default_error_handler(error: &Error, output: &mut dyn Write) {
use ansi_term::Colour::Red;
2020-03-21 19:35:04 +01:00
match error {
Error(ErrorKind::Io(ref io_error), _)
2020-03-30 22:18:41 +02:00
if io_error.kind() == ::std::io::ErrorKind::BrokenPipe =>
2020-03-21 19:35:04 +01:00
{
2020-03-30 22:18:41 +02:00
::std::process::exit(0);
2020-03-21 19:35:04 +01:00
}
Error(ErrorKind::SerdeYamlError(_), _) => {
writeln!(
output,
"{}: Error while parsing metadata.yaml file: {}",
Red.paint("[bat error]"),
error
)
.ok();
}
2020-03-21 19:35:04 +01:00
_ => {
writeln!(output, "{}: {}", Red.paint("[bat error]"), error).ok();
2020-03-21 19:35:04 +01:00
}
};
}