feat(snix/eval): proper error handling for invalid regexes
We don't want to crash the evaluator when an invalid regex is passed, but instead display a user-facing error message. This CL does that. Change-Id: I81fd8e342fc877344f8d2a3704ef53caf5190aa3 Reviewed-on: https://cl.snix.dev/c/snix/+/30588 Reviewed-by: Vova Kryachko <v.kryachko@gmail.com> Tested-by: besadii Reviewed-by: Bence Nemes <nemes.bence1@gmail.com>
This commit is contained in:
parent
4de1129acb
commit
3c1a7176cb
3 changed files with 12 additions and 3 deletions
|
|
@ -991,8 +991,9 @@ mod pure_builtins {
|
||||||
return Ok(re);
|
return Ok(re);
|
||||||
}
|
}
|
||||||
let re = re.to_str()?;
|
let re = re.to_str()?;
|
||||||
let re: Regex =
|
let re = re.to_str()?;
|
||||||
cached_regex(&format!("^{}$", re.to_str()?)).expect("TODO(tazjin): propagate error");
|
let re: Regex = cached_regex(&format!("^{}$", re))
|
||||||
|
.map_err(|_| ErrorKind::InvalidRegex(re.to_string()))?;
|
||||||
|
|
||||||
match re.captures(s.to_str()?) {
|
match re.captures(s.to_str()?) {
|
||||||
Some(caps) => Ok(Value::List(
|
Some(caps) => Ok(Value::List(
|
||||||
|
|
@ -1213,7 +1214,8 @@ mod pure_builtins {
|
||||||
let s = str.to_contextful_str()?;
|
let s = str.to_contextful_str()?;
|
||||||
let text = s.to_str()?;
|
let text = s.to_str()?;
|
||||||
let re = regex.to_str()?;
|
let re = regex.to_str()?;
|
||||||
let re = cached_regex(re.to_str()?).unwrap();
|
let re = re.to_str()?;
|
||||||
|
let re = cached_regex(re).map_err(|_| ErrorKind::InvalidRegex(re.to_string()))?;
|
||||||
let mut capture_locations = re.capture_locations();
|
let mut capture_locations = re.capture_locations();
|
||||||
let num_captures = capture_locations.len();
|
let num_captures = capture_locations.len();
|
||||||
let mut ret = Vec::new();
|
let mut ret = Vec::new();
|
||||||
|
|
|
||||||
|
|
@ -294,6 +294,10 @@ to a missing value in the attribute set(s) included via `with`."#
|
||||||
/// or "sha512"
|
/// or "sha512"
|
||||||
#[error("unknown hash type '{0}'")]
|
#[error("unknown hash type '{0}'")]
|
||||||
UnknownHashType(String),
|
UnknownHashType(String),
|
||||||
|
|
||||||
|
/// An invalid regular expression was passed.
|
||||||
|
#[error("invalid regular expression '{0}'")]
|
||||||
|
InvalidRegex(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl error::Error for Error {
|
impl error::Error for Error {
|
||||||
|
|
@ -688,6 +692,7 @@ impl Error {
|
||||||
| ErrorKind::WithContext { .. }
|
| ErrorKind::WithContext { .. }
|
||||||
| ErrorKind::UnknownHashType(_)
|
| ErrorKind::UnknownHashType(_)
|
||||||
| ErrorKind::InvalidHash(_)
|
| ErrorKind::InvalidHash(_)
|
||||||
|
| ErrorKind::InvalidRegex(_)
|
||||||
| ErrorKind::CatchableError(_) => return None,
|
| ErrorKind::CatchableError(_) => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -735,6 +740,7 @@ impl Error {
|
||||||
ErrorKind::UnknownHashType(_) => "E039",
|
ErrorKind::UnknownHashType(_) => "E039",
|
||||||
ErrorKind::UnexpectedArgumentBuiltin { .. } => "E040",
|
ErrorKind::UnexpectedArgumentBuiltin { .. } => "E040",
|
||||||
ErrorKind::InvalidHash(_) => "E041",
|
ErrorKind::InvalidHash(_) => "E041",
|
||||||
|
ErrorKind::InvalidRegex(_) => "E042",
|
||||||
|
|
||||||
// Special error code for errors from other Snix
|
// Special error code for errors from other Snix
|
||||||
// components. We may want to introduce a code namespacing
|
// components. We may want to introduce a code namespacing
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
builtins.match "a(b))c" "abc"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue