summaryrefslogtreecommitdiffstats
path: root/src/error.rs
blob: 2e73b85bbdbb62b6e8b4d70ffb196602b9ce4b2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use error_chain::error_chain;
use std::io::Write;

error_chain! {
    foreign_links {
        Clap(::clap::Error) #[cfg(feature = "application")];
        Io(::std::io::Error);
        SyntectError(::syntect::LoadingError);
        ParseIntError(::std::num::ParseIntError);
        GlobParsingError(::globset::Error);
        SerdeYamlError(::serde_yaml::Error);
    }

    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)
        }
    }
}

pub fn default_error_handler(error: &Error, output: &mut dyn Write) {
    use ansi_term::Colour::Red;

    match error {
        Error(ErrorKind::Io(ref io_error), _)
            if io_error.kind() == ::std::io::ErrorKind::BrokenPipe =>
        {
            ::std::process::exit(0);
        }
        Error(ErrorKind::SerdeYamlError(_), _) => {
            writeln!(
                output,
                "{}: Error while parsing metadata.yaml file: {}",
                Red.paint("[bat error]"),
                error
            )
            .ok();
        }
        _ => {
            writeln!(output, "{}: {}", Red.paint("[bat error]"), error).ok();
        }
    };
}