From c4c2c56042e7829aa9ee0c4b7fad93d039fb4841 Mon Sep 17 00:00:00 2001 From: Eric Harney Date: Tue, 23 Feb 2016 17:55:08 -0500 Subject: [PATCH] Don't print HTTP codes for non-HTTP errors This changes: $ cinder rename asdf ERROR: Must supply either name or description. (HTTP 1) To: $ cinder rename asdf ERROR: Must supply either name or description. Affects rename, snapshot-rename, consisgroup-update, and consisgroup-create-from-src. (consisgroup-* previously printed HTTP 400.) Closes-Bug: #1549020 Closes-Bug: #1549026 Change-Id: Ia920b3b75b53170789b694cbdd49100bd6a72d21 --- cinderclient/exceptions.py | 5 ++++- cinderclient/tests/unit/v2/test_shell.py | 21 +++++++++++++++++---- cinderclient/v2/shell.py | 6 +++--- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/cinderclient/exceptions.py b/cinderclient/exceptions.py index 3c79ceee7..dad3b184b 100644 --- a/cinderclient/exceptions.py +++ b/cinderclient/exceptions.py @@ -89,7 +89,10 @@ class ClientException(Exception): self.request_id = request_id def __str__(self): - formatted_string = "%s (HTTP %s)" % (self.message, self.code) + formatted_string = "%s" % self.message + if self.code >= 100: + # HTTP codes start at 100. + formatted_string += " (HTTP %s)" % self.code if self.request_id: formatted_string += " (Request-ID: %s)" % self.request_id diff --git a/cinderclient/tests/unit/v2/test_shell.py b/cinderclient/tests/unit/v2/test_shell.py index 03f25f689..af2889a2a 100644 --- a/cinderclient/tests/unit/v2/test_shell.py +++ b/cinderclient/tests/unit/v2/test_shell.py @@ -488,6 +488,14 @@ class ShellTest(utils.TestCase): # Call rename with no arguments self.assertRaises(SystemExit, self.run_command, 'rename') + def test_rename_invalid_args(self): + """Ensure that error generated does not reference an HTTP code.""" + + self.assertRaisesRegexp(exceptions.ClientException, + '(?!HTTP)', + self.run_command, + 'rename volume-1234-abcd') + def test_rename_snapshot(self): # basic rename with positional arguments self.run_command('snapshot-rename 1234 new-name') @@ -510,6 +518,11 @@ class ShellTest(utils.TestCase): # Call snapshot-rename with no arguments self.assertRaises(SystemExit, self.run_command, 'snapshot-rename') + def test_rename_snapshot_invalid_args(self): + self.assertRaises(exceptions.ClientException, + self.run_command, + 'snapshot-rename snapshot-1234') + def test_set_metadata_set(self): self.run_command('metadata 1234 set key1=val1 key2=val2') self.assert_called('POST', '/volumes/1234/metadata', @@ -1081,8 +1094,8 @@ class ShellTest(utils.TestCase): self.assert_called('PUT', '/consistencygroups/1234', body=expected) - def test_consistencygroup_update_bad_request(self): - self.assertRaises(exceptions.BadRequest, + def test_consistencygroup_update_invalid_args(self): + self.assertRaises(exceptions.ClientException, self.run_command, 'consisgroup-update 1234') @@ -1123,13 +1136,13 @@ class ShellTest(utils.TestCase): expected) def test_consistencygroup_create_from_src_fail_no_snap_cg(self): - self.assertRaises(exceptions.BadRequest, + self.assertRaises(exceptions.ClientException, self.run_command, 'consisgroup-create-from-src ' '--name cg') def test_consistencygroup_create_from_src_fail_both_snap_cg(self): - self.assertRaises(exceptions.BadRequest, + self.assertRaises(exceptions.ClientException, self.run_command, 'consisgroup-create-from-src ' '--name cg ' diff --git a/cinderclient/v2/shell.py b/cinderclient/v2/shell.py index b7b01d4a6..7f6f344f6 100644 --- a/cinderclient/v2/shell.py +++ b/cinderclient/v2/shell.py @@ -2352,11 +2352,11 @@ def do_consisgroup_create_from_src(cs, args): if not args.cgsnapshot and not args.source_cg: msg = ('Cannot create consistency group because neither ' 'cgsnapshot nor source CG is provided.') - raise exceptions.BadRequest(code=400, message=msg) + raise exceptions.ClientException(code=1, message=msg) if args.cgsnapshot and args.source_cg: msg = ('Cannot create consistency group because both ' 'cgsnapshot and source CG are provided.') - raise exceptions.BadRequest(code=400, message=msg) + raise exceptions.ClientException(code=1, message=msg) cgsnapshot = None if args.cgsnapshot: cgsnapshot = _find_cgsnapshot(cs, args.cgsnapshot) @@ -2438,7 +2438,7 @@ def do_consisgroup_update(cs, args): if not kwargs: msg = ('At least one of the following args must be supplied: ' 'name, description, add-volumes, remove-volumes.') - raise exceptions.BadRequest(code=400, message=msg) + raise exceptions.ClientException(code=1, message=msg) _find_consistencygroup(cs, args.consistencygroup).update(**kwargs)