2020-04-25 10:25:25 +00:00
|
|
|
use std::io::Write;
|
2021-08-26 11:12:21 +00:00
|
|
|
use thiserror::Error;
|
2020-03-21 18:35:04 +00:00
|
|
|
|
2021-08-26 11:12:21 +00:00
|
|
|
#[derive(Error, Debug)]
|
2022-05-07 11:43:11 +00:00
|
|
|
#[non_exhaustive]
|
2021-08-26 11:12:21 +00:00
|
|
|
pub enum Error {
|
|
|
|
#[error(transparent)]
|
|
|
|
Io(#[from] ::std::io::Error),
|
|
|
|
#[error(transparent)]
|
2022-05-07 11:43:11 +00:00
|
|
|
SyntectError(#[from] ::syntect::Error),
|
|
|
|
#[error(transparent)]
|
|
|
|
SyntectLoadingError(#[from] ::syntect::LoadingError),
|
2021-08-26 11:12:21 +00:00
|
|
|
#[error(transparent)]
|
|
|
|
ParseIntError(#[from] ::std::num::ParseIntError),
|
|
|
|
#[error(transparent)]
|
|
|
|
GlobParsingError(#[from] ::globset::Error),
|
|
|
|
#[error(transparent)]
|
|
|
|
SerdeYamlError(#[from] ::serde_yaml::Error),
|
|
|
|
#[error("unable to detect syntax for {0}")]
|
|
|
|
UndetectedSyntax(String),
|
|
|
|
#[error("unknown syntax: '{0}'")]
|
|
|
|
UnknownSyntax(String),
|
|
|
|
#[error("Unknown style '{0}'")]
|
|
|
|
UnknownStyle(String),
|
|
|
|
#[error("Use of bat as a pager is disallowed in order to avoid infinite recursion problems")]
|
|
|
|
InvalidPagerValueBat,
|
|
|
|
#[error("{0}")]
|
|
|
|
Msg(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&'static str> for Error {
|
|
|
|
fn from(s: &'static str) -> Self {
|
|
|
|
Error::Msg(s.to_owned())
|
2020-03-21 18:35:04 +00:00
|
|
|
}
|
2021-08-26 11:12:21 +00:00
|
|
|
}
|
2020-05-16 00:52:33 +00:00
|
|
|
|
2021-08-26 11:12:21 +00:00
|
|
|
impl From<String> for Error {
|
|
|
|
fn from(s: String) -> Self {
|
|
|
|
Error::Msg(s)
|
2020-05-16 00:52:33 +00:00
|
|
|
}
|
2020-03-21 18:35:04 +00:00
|
|
|
}
|
|
|
|
|
2021-08-26 11:12:21 +00:00
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
2020-04-25 10:25:25 +00:00
|
|
|
pub fn default_error_handler(error: &Error, output: &mut dyn Write) {
|
2020-04-21 13:50:46 +00:00
|
|
|
use ansi_term::Colour::Red;
|
|
|
|
|
2020-03-21 18:35:04 +00:00
|
|
|
match error {
|
2021-08-26 11:12:21 +00:00
|
|
|
Error::Io(ref io_error) if io_error.kind() == ::std::io::ErrorKind::BrokenPipe => {
|
2020-03-30 20:18:41 +00:00
|
|
|
::std::process::exit(0);
|
2020-03-21 18:35:04 +00:00
|
|
|
}
|
2021-08-26 11:12:21 +00:00
|
|
|
Error::SerdeYamlError(_) => {
|
2020-04-25 10:25:25 +00:00
|
|
|
writeln!(
|
|
|
|
output,
|
2020-04-21 13:50:46 +00:00
|
|
|
"{}: Error while parsing metadata.yaml file: {}",
|
|
|
|
Red.paint("[bat error]"),
|
|
|
|
error
|
2020-04-25 10:25:25 +00:00
|
|
|
)
|
|
|
|
.ok();
|
2020-04-21 13:50:46 +00:00
|
|
|
}
|
2020-03-21 18:35:04 +00:00
|
|
|
_ => {
|
2020-04-25 10:25:25 +00:00
|
|
|
writeln!(output, "{}: {}", Red.paint("[bat error]"), error).ok();
|
2020-03-21 18:35:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|