Skip to content

Commit 5619dec

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add status_code to HttpException"
2 parents be162a8 + 81aa63d commit 5619dec

File tree

4 files changed

+66
-8
lines changed

4 files changed

+66
-8
lines changed

openstack/exceptions.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
class SDKException(Exception):
2323
"""The base exception class for all exceptions this library raises."""
2424
def __init__(self, message=None):
25-
message = self.__class__.__name__ if message is None else message
26-
super(Exception, self).__init__(message)
25+
self.message = self.__class__.__name__ if message is None else message
26+
super(Exception, self).__init__(self.message)
2727

2828

2929
class AuthorizationFailure(SDKException):
@@ -60,9 +60,10 @@ def __init__(self, response):
6060

6161

6262
class HttpException(SDKException):
63-
def __init__(self, message, details=None):
63+
def __init__(self, message, details=None, status_code=None):
6464
super(HttpException, self).__init__(message)
6565
self.details = details
66+
self.status_code = status_code
6667

6768
def __unicode__(self):
6869
msg = self.__class__.__name__ + ": " + self.message

openstack/tests/test_exceptions.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
import testtools
14+
15+
from openstack import exceptions
16+
17+
18+
class Test_HttpException(testtools.TestCase):
19+
20+
def setUp(self):
21+
super(Test_HttpException, self).setUp()
22+
self.message = "mayday"
23+
24+
def _do_raise(self, *args, **kwargs):
25+
raise exceptions.HttpException(*args, **kwargs)
26+
27+
def test_message(self):
28+
exc = self.assertRaises(exceptions.HttpException,
29+
self._do_raise, self.message)
30+
31+
self.assertEqual(self.message, exc.message)
32+
33+
def test_details(self):
34+
details = "some details"
35+
exc = self.assertRaises(exceptions.HttpException,
36+
self._do_raise, self.message,
37+
details=details)
38+
39+
self.assertEqual(self.message, exc.message)
40+
self.assertEqual(details, exc.details)
41+
42+
def test_status_code(self):
43+
status_code = 123
44+
exc = self.assertRaises(exceptions.HttpException,
45+
self._do_raise, self.message,
46+
status_code=status_code)
47+
48+
self.assertEqual(self.message, exc.message)
49+
self.assertEqual(status_code, exc.status_code)

openstack/tests/test_transport.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,22 @@ def test_verify_arg_file(self):
270270
@httpretty.activate
271271
def test_not_found(self):
272272
xport = transport.Transport()
273-
self.stub_url(httpretty.GET, status=404)
273+
status = 404
274+
self.stub_url(httpretty.GET, status=status)
274275

275-
self.assertRaises(exceptions.HttpException, xport.get, self.TEST_URL)
276+
exc = self.assertRaises(exceptions.HttpException, xport.get,
277+
self.TEST_URL)
278+
self.assertEqual(status, exc.status_code)
276279

277280
@httpretty.activate
278281
def test_server_error(self):
279282
xport = transport.Transport()
283+
status = 500
280284
self.stub_url(httpretty.GET, status=500)
281285

282-
self.assertRaises(exceptions.HttpException, xport.get, self.TEST_URL)
286+
exc = self.assertRaises(exceptions.HttpException, xport.get,
287+
self.TEST_URL)
288+
self.assertEqual(status, exc.status_code)
283289

284290

285291
class TestTransportDebug(base.TestTransportBase):

openstack/transport.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,10 @@ def request(self, method, url, redirect=None, **kwargs):
262262
try:
263263
resp.raise_for_status()
264264
except requests.RequestException as e:
265-
raise exceptions.HttpException(six.text_type(e),
266-
self._parse_error_response(resp))
265+
raise exceptions.HttpException(
266+
six.text_type(e),
267+
details=self._parse_error_response(resp),
268+
status_code=resp.status_code)
267269
if accept == JSON:
268270
try:
269271
resp.body = resp.json()

0 commit comments

Comments
 (0)