on 4236fa9 help sign in
bat/src/error.rs
62 lines 1.7 kB history raw
1use std::io::Write;
2use thiserror::Error;
4#[derive(Error, Debug)]
5pub enum Error {
6 #[error(transparent)]
7 Io(#[from] ::std::io::Error),
8 #[error(transparent)]
9 SyntectError(#[from] ::syntect::LoadingError),
10 #[error(transparent)]
11 ParseIntError(#[from] ::std::num::ParseIntError),
12 #[error(transparent)]
13 GlobParsingError(#[from] ::globset::Error),
14 #[error(transparent)]
15 SerdeYamlError(#[from] ::serde_yaml::Error),
16 #[error("unable to detect syntax for {0}")]
17 UndetectedSyntax(String),
18 #[error("unknown syntax: '{0}'")]
19 UnknownSyntax(String),
20 #[error("Unknown style '{0}'")]
21 UnknownStyle(String),
22 #[error("Use of bat as a pager is disallowed in order to avoid infinite recursion problems")]
23 InvalidPagerValueBat,
24 #[error("{0}")]
25 Msg(String),
28impl From<&'static str> for Error {
29 fn from(s: &'static str) -> Self {
30 Error::Msg(s.to_owned())
31 }
34impl From<String> for Error {
35 fn from(s: String) -> Self {
36 Error::Msg(s)
37 }
40pub type Result<T> = std::result::Result<T, Error>;
42pub fn default_error_handler(error: &Error, output: &mut dyn Write) {
43 use ansi_term::Colour::Red;
45 match error {
46 Error::Io(ref io_error) if io_error.kind() == ::std::io::ErrorKind::BrokenPipe => {
47 ::std::process::exit(0);
48 }
49 Error::SerdeYamlError(_) => {
50 writeln!(
51 output,
52 "{}: Error while parsing metadata.yaml file: {}",
53 Red.paint("[bat error]"),
54 error
55 )
56 .ok();
57 }
58 _ => {
59 writeln!(output, "{}: {}", Red.paint("[bat error]"), error).ok();
60 }
61 };