2020-04-25 12:25:25 +02:00
|
|
|
use std::io::Write;
|
2021-08-26 13:12:21 +02:00
|
|
|
use thiserror::Error;
|
2020-03-21 19:35:04 +01:00
|
|
|
|
2021-08-26 13:12:21 +02:00
|
|
|
#[derive(Error, Debug)]
|
2022-05-07 13:43:11 +02:00
|
|
|
#[non_exhaustive]
|
2021-08-26 13:12:21 +02:00
|
|
|
pub enum Error {
|
|
|
|
#[error(transparent)]
|
|
|
|
Io(#[from] ::std::io::Error),
|
|
|
|
#[error(transparent)]
|
2023-07-09 02:08:14 +02:00
|
|
|
Fmt(#[from] ::std::fmt::Error),
|
|
|
|
#[error(transparent)]
|
2022-05-07 13:43:11 +02:00
|
|
|
SyntectError(#[from] ::syntect::Error),
|
|
|
|
#[error(transparent)]
|
|
|
|
SyntectLoadingError(#[from] ::syntect::LoadingError),
|
2021-08-26 13:12:21 +02: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),
|
2023-09-02 08:48:26 +02:00
|
|
|
#[cfg(feature = "lessopen")]
|
|
|
|
#[error(transparent)]
|
|
|
|
VarError(#[from] ::std::env::VarError),
|
|
|
|
#[cfg(feature = "lessopen")]
|
|
|
|
#[error(transparent)]
|
|
|
|
CommandParseError(#[from] ::shell_words::ParseError),
|
2021-08-26 13:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&'static str> for Error {
|
|
|
|
fn from(s: &'static str) -> Self {
|
|
|
|
Error::Msg(s.to_owned())
|
2020-03-21 19:35:04 +01:00
|
|
|
}
|
2021-08-26 13:12:21 +02:00
|
|
|
}
|
2020-05-16 02:52:33 +02:00
|
|
|
|
2021-08-26 13:12:21 +02:00
|
|
|
impl From<String> for Error {
|
|
|
|
fn from(s: String) -> Self {
|
|
|
|
Error::Msg(s)
|
2020-05-16 02:52:33 +02:00
|
|
|
}
|
2020-03-21 19:35:04 +01:00
|
|
|
}
|
|
|
|
|
2021-08-26 13:12:21 +02:00
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
2020-04-25 12:25:25 +02:00
|
|
|
pub fn default_error_handler(error: &Error, output: &mut dyn Write) {
|
2023-03-23 10:47:38 +01:00
|
|
|
use nu_ansi_term::Color::Red;
|
2020-04-21 15:50:46 +02:00
|
|
|
|
2020-03-21 19:35:04 +01:00
|
|
|
match error {
|
2021-08-26 13:12:21 +02:00
|
|
|
Error::Io(ref io_error) if io_error.kind() == ::std::io::ErrorKind::BrokenPipe => {
|
2020-03-30 22:18:41 +02:00
|
|
|
::std::process::exit(0);
|
2020-03-21 19:35:04 +01:00
|
|
|
}
|
2021-08-26 13:12:21 +02:00
|
|
|
Error::SerdeYamlError(_) => {
|
2020-04-25 12:25:25 +02:00
|
|
|
writeln!(
|
|
|
|
output,
|
2020-04-21 15:50:46 +02:00
|
|
|
"{}: Error while parsing metadata.yaml file: {}",
|
|
|
|
Red.paint("[bat error]"),
|
|
|
|
error
|
2020-04-25 12:25:25 +02:00
|
|
|
)
|
|
|
|
.ok();
|
2020-04-21 15:50:46 +02:00
|
|
|
}
|
2020-03-21 19:35:04 +01:00
|
|
|
_ => {
|
2020-04-25 12:25:25 +02:00
|
|
|
writeln!(output, "{}: {}", Red.paint("[bat error]"), error).ok();
|
2020-03-21 19:35:04 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|