brush_core/
error.rs

1use std::path::PathBuf;
2
3/// Monolithic error type for the shell
4#[derive(thiserror::Error, Debug)]
5pub enum Error {
6    /// A local variable was set outside of a function
7    #[error("can't set local variable outside of function")]
8    SetLocalVarOutsideFunction,
9
10    /// A tilde expression was used without a valid HOME variable
11    #[error("cannot expand tilde expression with HOME not set")]
12    TildeWithoutValidHome,
13
14    /// An attempt was made to assign a list to an array member
15    #[error("cannot assign list to array member")]
16    AssigningListToArrayMember,
17
18    /// An attempt was made to convert an associative array to an indexed array.
19    #[error("cannot convert associative array to indexed array")]
20    ConvertingAssociativeArrayToIndexedArray,
21
22    /// An attempt was made to convert an indexed array to an associative array.
23    #[error("cannot convert indexed array to associative array")]
24    ConvertingIndexedArrayToAssociativeArray,
25
26    /// An error occurred while sourcing the indicated script file.
27    #[error("failed to source file: {0}; {1}")]
28    FailedSourcingFile(PathBuf, Box<Error>),
29
30    /// The shell failed to send a signal to a process.
31    #[error("failed to send signal to process")]
32    FailedToSendSignal,
33
34    /// An attempt was made to assign a value to a special parameter.
35    #[error("cannot assign in this way")]
36    CannotAssignToSpecialParameter,
37
38    /// Checked expansion error.
39    #[error("expansion error: {0}")]
40    CheckedExpansionError(String),
41
42    /// A reference was made to an unknown shell function.
43    #[error("function not found: {0}")]
44    FunctionNotFound(String),
45
46    /// Command was not found.
47    #[error("command not found: {0}")]
48    CommandNotFound(String),
49
50    /// The requested functionality has not yet been implemented in this shell.
51    #[error("UNIMPLEMENTED: {0}")]
52    Unimplemented(&'static str),
53
54    /// An expected environment scope could not be found.
55    #[error("missing scope")]
56    MissingScope,
57
58    /// The given path is not a directory.
59    #[error("not a directory: {0}")]
60    NotADirectory(PathBuf),
61
62    /// The given path is a directory.
63    #[error("path is a directory")]
64    IsADirectory,
65
66    /// The given variable is not an array.
67    #[error("variable is not an array")]
68    NotArray,
69
70    /// The current user could not be determined.
71    #[error("no current user")]
72    NoCurrentUser,
73
74    /// The requested input or output redirection is invalid.
75    #[error("invalid redirection")]
76    InvalidRedirection,
77
78    /// An error occurred while redirecting input or output with the given file.
79    #[error("failed to redirect to {0}: {1}")]
80    RedirectionFailure(String, std::io::Error),
81
82    /// An error occurred evaluating an arithmetic expression.
83    #[error("arithmetic evaluation error: {0}")]
84    EvalError(#[from] crate::arithmetic::EvalError),
85
86    /// The given string could not be parsed as an integer.
87    #[error("failed to parse integer")]
88    IntParseError(#[from] std::num::ParseIntError),
89
90    /// The given string could not be parsed as an integer.
91    #[error("failed to parse integer")]
92    TryIntParseError(#[from] std::num::TryFromIntError),
93
94    /// A byte sequence could not be decoded as a valid UTF-8 string.
95    #[error("failed to decode utf-8")]
96    FromUtf8Error(#[from] std::string::FromUtf8Error),
97
98    /// A byte sequence could not be decoded as a valid UTF-8 string.
99    #[error("failed to decode utf-8")]
100    Utf8Error(#[from] std::str::Utf8Error),
101
102    /// An attempt was made to modify a readonly variable.
103    #[error("cannot mutate readonly variable")]
104    ReadonlyVariable,
105
106    /// The indicated pattern is invalid.
107    #[error("invalid pattern: '{0}'")]
108    InvalidPattern(String),
109
110    /// A regular expression error occurred
111    #[error("regex error: {0}")]
112    RegexError(#[from] fancy_regex::Error),
113
114    /// An invalid regular expression was provided.
115    #[error("invalid regex: {0}; expression: '{1}'")]
116    InvalidRegexError(fancy_regex::Error, String),
117
118    /// An I/O error occurred.
119    #[error("i/o error: {0}")]
120    IoError(#[from] std::io::Error),
121
122    /// Invalid substitution syntax.
123    #[error("bad substitution")]
124    BadSubstitution,
125
126    /// Invalid arguments were provided to the command.
127    #[error("invalid arguments")]
128    InvalidArguments,
129
130    /// An error occurred while creating a child process.
131    #[error("failed to create child process")]
132    ChildCreationFailure,
133
134    /// An error occurred while formatting a string.
135    #[error("{0}")]
136    FormattingError(#[from] std::fmt::Error),
137
138    /// An error occurred while parsing a word.
139    #[error("{0}")]
140    WordParseError(#[from] brush_parser::WordParseError),
141
142    /// Unable to parse a test command.
143    #[error("{0}")]
144    TestCommandParseError(#[from] brush_parser::TestCommandParseError),
145
146    /// A threading error occurred.
147    #[error("threading error")]
148    ThreadingError(#[from] tokio::task::JoinError),
149
150    /// An invalid signal was referenced.
151    #[error("{0}: invalid signal specification")]
152    InvalidSignal(String),
153
154    /// A system error occurred.
155    #[cfg(unix)]
156    #[error("system error: {0}")]
157    ErrnoError(#[from] nix::errno::Errno),
158
159    /// An invalid umask was provided.
160    #[error("invalid umask value")]
161    InvalidUmask,
162
163    /// An error occurred reading from procfs.
164    #[cfg(target_os = "linux")]
165    #[error("procfs error: {0}")]
166    ProcfsError(#[from] procfs::ProcError),
167
168    /// The given open file cannot be read from.
169    #[error("cannot read from {0}")]
170    OpenFileNotReadable(&'static str),
171
172    /// The given open file cannot be written to.
173    #[error("cannot write to {0}")]
174    OpenFileNotWritable(&'static str),
175
176    /// Bad file descriptor.
177    #[error("bad file descriptor: {0}")]
178    BadFileDescriptor(u32),
179
180    /// Printf failure
181    #[error("printf failure: {0}")]
182    PrintfFailure(i32),
183
184    /// Interrupted
185    #[error("interrupted")]
186    Interrupted,
187
188    /// Maximum function call depth was exceeded.
189    #[error("maximum function call depth exceeded")]
190    MaxFunctionCallDepthExceeded,
191
192    /// System time error.
193    #[error("system time error: {0}")]
194    TimeError(#[from] std::time::SystemTimeError),
195}
196
197/// Convenience function for returning an error for unimplemented functionality.
198///
199/// # Arguments
200///
201/// * `msg` - The message to include in the error
202pub(crate) fn unimp<T>(msg: &'static str) -> Result<T, Error> {
203    Err(Error::Unimplemented(msg))
204}