browserslist/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error, PartialEq, Eq, Clone)]
4/// The errors may occur when querying with browserslist.
5pub enum Error {
6    #[error("failed to parse the rest of input: ...'{0}'")]
7    /// Error of parsing query.
8    Nom(String),
9
10    #[error("invalid date: {0}")]
11    /// Date format is invalid.
12    InvalidDate(String),
13
14    #[error("query cannot start with 'not'; add any other queries before '{0}'")]
15    /// Query can't start with a negated query which starts with `not`.
16    NotAtFirst(String),
17
18    #[error("unknown browser: '{0}'")]
19    /// The given browser name can't be found.
20    BrowserNotFound(String),
21
22    #[error("unknown Electron version: {0}")]
23    /// The given Electron version can't be found.
24    UnknownElectronVersion(String),
25
26    #[error("unknown Node.js version: {0}")]
27    /// The given Node.js version can't be found.
28    UnknownNodejsVersion(String),
29
30    #[error("unknown version '{1}' of browser '{0}'")]
31    /// The given version of the given browser can't be found.
32    UnknownBrowserVersion(String, String),
33
34    #[error("current environment for querying `current node` is not supported")]
35    /// Current environment doesn't support querying `current node`,
36    /// for example, running this library on Non-Node.js platform or
37    /// no Node.js installed.
38    UnsupportedCurrentNode,
39
40    #[error("current environment for querying `extends ...` is not supported")]
41    /// Current environment doesn't support querying `extends`.
42    UnsupportedExtends,
43
44    #[error("unknown browser feature: '{0}'")]
45    /// Unknown browser feature.
46    UnknownBrowserFeature(String),
47
48    #[error("unknown region: '{0}'")]
49    /// Unknown Can I Use region.
50    UnknownRegion(String),
51
52    #[error("unknown browser query: '{0}'")]
53    /// Query can't be recognized.
54    UnknownQuery(String),
55
56    #[error("duplicated section '{0}' in config")]
57    /// Duplicated section in configuration.
58    DuplicatedSection(String),
59
60    #[error("failed to read config file: {0}")]
61    /// Failed to read config.
62    FailedToReadConfig(String),
63
64    #[error("missing 'browserslist' field in '{0}' file")]
65    /// Missing `browserslist` field in `package.json` file.
66    MissingFieldInPkg(String),
67
68    #[error("duplicated: '{0}' directory contains both {1} and {2}.")]
69    /// Duplicated configuration found.
70    DuplicatedConfig(String, &'static str, &'static str),
71
72    #[error("failed to access current working directory")]
73    /// Failed to access the current working directory.
74    FailedToAccessCurrentDir,
75
76    #[error("missing config for Browserslist environment '{0}'")]
77    /// Missing config corresponding to specific environment.
78    MissingEnv(String),
79
80    #[error("invalid extend name: {0}")]
81    /// Invalid extend name
82    InvalidExtendName(&'static str),
83
84    #[error("failed to resolve '{0}' package in `extends` query")]
85    /// Failed to resolve package in `extends` query.
86    FailedToResolveExtend(String),
87
88    #[error("year overflow")]
89    /// Year overflow.
90    YearOverflow,
91}
92
93impl<'a> From<nom::Err<nom::error::Error<&'a str>>> for Error {
94    fn from(e: nom::Err<nom::error::Error<&'a str>>) -> Self {
95        match e {
96            nom::Err::Error(e) | nom::Err::Failure(e) => Self::Nom(e.input.to_owned()),
97            _ => unreachable!(),
98        }
99    }
100}