Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error message for unknown keys #935

Merged
merged 3 commits into from
Oct 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Next Release (TBD)
* bugfix:S3 Response Parsing: Fix regression for parsing S3 responses
containing a status code of 200 with an error response body
(`botocore issue 342 <https://github.com/boto/botocore/pull/342>`__)
* bugfix:Shorthand Error Message: Ensure the error message for
shorthand parsing always contains the CLI argument name
(`issue 935 <https://github.com/aws/aws-cli/pull/935>`__)


1.5.0
Expand Down
14 changes: 7 additions & 7 deletions awscli/argprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ class ParamSyntaxError(Exception):


class ParamUnknownKeyError(Exception):
def __init__(self, param, key, valid_keys):
def __init__(self, key, valid_keys):
valid_keys = ', '.join(valid_keys)
full_message = (
"Unknown key '%s' for parameter %s, valid choices "
"are: %s" % (key, '--%s' % xform_name(param.name), valid_keys))
"Unknown key '%s', valid choices "
"are: %s" % (key, valid_keys))
super(ParamUnknownKeyError, self).__init__(full_message)


Expand Down Expand Up @@ -284,12 +284,12 @@ def __call__(self, cli_argument, value, **kwargs):
docgen = ParamShorthandDocGen()
example_usage = docgen.generate_shorthand_example(cli_argument)
raise ParamError(cli_argument.cli_name, "should be: %s" % example_usage)
except ParamError as e:
except (ParamError, ParamUnknownKeyError) as e:
# The shorthand parse methods don't have the cli_name,
# so any ParamError won't have this value. To accomodate
# this, ParamErrors are caught and reraised with the cli_name
# injected.
raise ParamError(cli_argument.cli_name, e.message)
raise ParamError(cli_argument.cli_name, str(e))
return parsed

def get_parse_method_for_param(self, cli_argument, value=None):
Expand Down Expand Up @@ -362,7 +362,7 @@ def _struct_scalar_list_parse(self, param, value):
# This is a key/value pair.
current_key = current[0].strip()
if current_key not in args:
raise ParamUnknownKeyError(param, current_key,
raise ParamUnknownKeyError(current_key,
args.keys())
current_value = unpack_scalar_cli_arg(args[current_key],
current[1].strip())
Expand Down Expand Up @@ -440,7 +440,7 @@ def _key_value_parse(self, param, value):
key = key.strip()
value = value.strip()
if valid_names and key not in valid_names:
raise ParamUnknownKeyError(param, key, valid_names)
raise ParamUnknownKeyError(key, valid_names)
if valid_names:
sub_param = valid_names[key]
if sub_param is not None:
Expand Down
15 changes: 11 additions & 4 deletions tests/unit/test_argprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,15 @@ def test_error_messages_for_structure_scalar(self):
def test_mispelled_param_name(self):
p = self.get_param_model(
'elasticbeanstalk.CreateConfigurationTemplate.SourceConfiguration')
error_msg = 'valid choices.*ApplicationName'
with self.assertRaisesRegexp(ParamUnknownKeyError, error_msg):
# We're checking three things.
# 1) The CLI parameter is in the error message
# 2) The parameter name that failed validation is in the error message
# 3) The correct parameter name is in the error message.
error_msg = (
'--source-configuration.*'
'ApplicationNames.*valid choices.*'
'ApplicationName')
with self.assertRaisesRegexp(ParamError, error_msg):
# Typo in 'ApplicationName'
self.simplify(p, 'ApplicationNames=foo, TemplateName=bar')

Expand All @@ -312,8 +319,8 @@ def test_improper_separator_for_filters_param(self):

def test_unknown_key_for_filters_param(self):
p = self.get_param_model('ec2.DescribeInstances.Filters')
with self.assertRaisesRegexp(ParamUnknownKeyError,
'valid choices.*Name'):
with self.assertRaisesRegexp(ParamError,
'--filters.*Names.*valid choices.*Name'):
self.simplify(p, ["Names=instance-id,Values=foo,bar"])

def test_csv_syntax_escaped(self):
Expand Down