cliparser: add an error type about illegal alias

Summary:
When the alias cannot be split via shlex::split, raise an error directly
instead of silently ignoring it and fallback to Python error handling.

Reviewed By: kulshrax

Differential Revision: D16530519

fbshipit-source-id: d81534b198555b256f062dc4e6520fa40ace7700
This commit is contained in:
Jun Wu 2019-07-29 19:30:30 -07:00 committed by Facebook Github Bot
parent e2dc067ee0
commit 5dd0629478
5 changed files with 16 additions and 8 deletions

View File

@ -1058,6 +1058,9 @@ def _parse(ui, args):
except cliparser.CircularReference as e:
alias = e.args[1]
raise error.Abort(_("circular alias: %s") % alias)
except cliparser.IllformedAlias as e:
msg = e.args[0]
raise error.Abort(msg)
else:
replacement = []

View File

@ -16,6 +16,7 @@ mod exceptions {
py_exception!(cliparser, AmbiguousCommand);
py_exception!(cliparser, CircularReference);
py_exception!(cliparser, IllformedAlias);
py_exception!(cliparser, OptionNotRecognized);
py_exception!(cliparser, OptionRequiresArgument);
py_exception!(cliparser, OptionArgumentInvalid);
@ -58,6 +59,7 @@ pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> {
use exceptions::*;
m.add(py, "AmbiguousCommand", AmbiguousCommand::type_object(py))?;
m.add(py, "CircularReference", CircularReference::type_object(py))?;
m.add(py, "IllformedAlias", IllformedAlias::type_object(py))?;
m.add(
py,
"OptionNotRecognized",
@ -256,5 +258,8 @@ fn map_to_python_err(py: Python, err: ParseError) -> PyErr {
ParseError::CircularReference { command_name } => {
return PyErr::new::<exceptions::CircularReference, _>(py, (msg, command_name))
}
ParseError::IllformedAlias { name, value } => {
return PyErr::new::<exceptions::IllformedAlias, _>(py, (msg, name, value));
}
}
}

View File

@ -43,13 +43,11 @@ pub fn expand_aliases(
loop {
match cfg.get(&arg) {
Some(alias) => {
let parts: Vec<String> = match split(alias) {
Some(v) => v,
None => {
expanded.push(arg);
break;
}
};
let parts: Vec<String> =
split(alias).ok_or_else(|| ParseError::IllformedAlias {
name: arg.clone(),
value: alias.to_string(),
})?;
if !visited.insert(arg.clone()) {
return Err(ParseError::CircularReference { command_name: arg });

View File

@ -68,6 +68,8 @@ pub enum ParseError {
},
#[fail(display = "Alias {} resulted in a circular reference", command_name)]
CircularReference { command_name: String },
#[fail(display = "alias definition {} = {:?} cannot be parsed", name, value)]
IllformedAlias { name: String, value: String },
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]

View File

@ -231,7 +231,7 @@ no definition
no closing quotation
$ hg noclosing
abort: error in definition for alias 'noclosingquotation': No closing quotation
abort: alias definition noclosingquotation = "\'" cannot be parsed
[255]
$ hg help noclosing
error in definition for alias 'noclosingquotation': No closing quotation