bat/src/error.rs

66 lines
1.8 KiB
Rust
Raw Normal View History

use std::io::Write;
use thiserror::Error;
2020-03-21 18:35:04 +00:00
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error(transparent)]
Io(#[from] ::std::io::Error),
#[error(transparent)]
SyntectError(#[from] ::syntect::Error),
#[error(transparent)]
SyntectLoadingError(#[from] ::syntect::LoadingError),
#[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
}
}
impl From<String> for Error {
fn from(s: String) -> Self {
Error::Msg(s)
}
2020-03-21 18:35:04 +00:00
}
pub type Result<T> = std::result::Result<T, Error>;
pub fn default_error_handler(error: &Error, output: &mut dyn Write) {
use ansi_term::Colour::Red;
2020-03-21 18:35:04 +00:00
match error {
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
}
Error::SerdeYamlError(_) => {
writeln!(
output,
"{}: Error while parsing metadata.yaml file: {}",
Red.paint("[bat error]"),
error
)
.ok();
}
2020-03-21 18:35:04 +00:00
_ => {
writeln!(output, "{}: {}", Red.paint("[bat error]"), error).ok();
2020-03-21 18:35:04 +00:00
}
};
}