Skip to content

Commit ee23913

Browse files
authored
Merge pull request #179 from betamos/master
Add subclasses for HttpError for simplified control flow
2 parents 7aff8d8 + 22e9a5c commit ee23913

File tree

5 files changed

+118
-14
lines changed

5 files changed

+118
-14
lines changed

apitools/base/py/base_api.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,9 +595,8 @@ def __ProcessHttpResponse(self, method_config, http_response, request):
595595
"""Process the given http response."""
596596
if http_response.status_code not in (http_client.OK,
597597
http_client.NO_CONTENT):
598-
raise exceptions.HttpError(
599-
http_response.info, http_response.content,
600-
http_response.request_url, method_config, request)
598+
raise exceptions.HttpError.FromResponse(
599+
http_response, method_config=method_config, request=request)
601600
if http_response.status_code == http_client.NO_CONTENT:
602601
# TODO(craigcitro): Find out why _replace doesn't seem to work
603602
# here.

apitools/base/py/base_api_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,9 @@ def fakeMakeRequest(*unused_args, **unused_kwargs):
198198
service = FakeService(client=client)
199199
request = SimpleMessage()
200200
with mock(base_api.http_wrapper, 'MakeRequest', fakeMakeRequest):
201-
with self.assertRaises(exceptions.HttpError) as error_context:
201+
with self.assertRaises(exceptions.HttpBadRequestError) as err:
202202
service._RunMethod(method_config, request)
203-
http_error = error_context.exception
204-
self.assertEquals(400, http_error.status_code)
203+
http_error = err.exception
205204
self.assertEquals('http://www.google.com', http_error.url)
206205
self.assertEquals('{"field": "abc"}', http_error.content)
207206
self.assertEquals(method_config, http_error.method_config)

apitools/base/py/exceptions.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,43 @@ def status_code(self):
7474
return int(self.response['status'])
7575

7676
@classmethod
77-
def FromResponse(cls, http_response):
78-
return cls(http_response.info, http_response.content,
79-
http_response.request_url)
77+
def FromResponse(cls, http_response, **kwargs):
78+
try:
79+
status_code = int(http_response.info.get('status'))
80+
error_cls = _HTTP_ERRORS.get(status_code, cls)
81+
except ValueError:
82+
error_cls = cls
83+
return error_cls(http_response.info, http_response.content,
84+
http_response.request_url, **kwargs)
85+
86+
87+
class HttpBadRequestError(HttpError):
88+
"""HTTP 400 Bad Request."""
89+
90+
91+
class HttpUnauthorizedError(HttpError):
92+
"""HTTP 401 Unauthorized."""
93+
94+
95+
class HttpForbiddenError(HttpError):
96+
"""HTTP 403 Forbidden."""
97+
98+
99+
class HttpNotFoundError(HttpError):
100+
"""HTTP 404 Not Found."""
101+
102+
103+
class HttpConflictError(HttpError):
104+
"""HTTP 409 Conflict."""
105+
106+
107+
_HTTP_ERRORS = {
108+
400: HttpBadRequestError,
109+
401: HttpUnauthorizedError,
110+
403: HttpForbiddenError,
111+
404: HttpNotFoundError,
112+
409: HttpConflictError,
113+
}
80114

81115

82116
class InvalidUserInputError(InvalidDataError):
@@ -143,14 +177,15 @@ class RetryAfterError(HttpError):
143177

144178
"""The response contained a retry-after header."""
145179

146-
def __init__(self, response, content, url, retry_after):
147-
super(RetryAfterError, self).__init__(response, content, url)
180+
def __init__(self, response, content, url, retry_after, **kwargs):
181+
super(RetryAfterError, self).__init__(response, content, url, **kwargs)
148182
self.retry_after = int(retry_after)
149183

150184
@classmethod
151-
def FromResponse(cls, http_response):
185+
def FromResponse(cls, http_response, **kwargs):
152186
return cls(http_response.info, http_response.content,
153-
http_response.request_url, http_response.retry_after)
187+
http_response.request_url, http_response.retry_after,
188+
**kwargs)
154189

155190

156191
class BadStatusCodeError(HttpError):
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright 2017 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest2
16+
17+
from apitools.base.py import exceptions
18+
from apitools.base.py import http_wrapper
19+
20+
21+
def _MakeResponse(status_code):
22+
return http_wrapper.Response(
23+
info={'status': status_code}, content='{"field": "abc"}',
24+
request_url='http://www.google.com')
25+
26+
27+
class HttpErrorFromResponseTest(unittest2.TestCase):
28+
29+
"""Tests for exceptions.HttpError.FromResponse."""
30+
31+
def testBadRequest(self):
32+
err = exceptions.HttpError.FromResponse(_MakeResponse(400))
33+
self.assertIsInstance(err, exceptions.HttpError)
34+
self.assertIsInstance(err, exceptions.HttpBadRequestError)
35+
self.assertEquals(err.status_code, 400)
36+
37+
def testUnauthorized(self):
38+
err = exceptions.HttpError.FromResponse(_MakeResponse(401))
39+
self.assertIsInstance(err, exceptions.HttpError)
40+
self.assertIsInstance(err, exceptions.HttpUnauthorizedError)
41+
self.assertEquals(err.status_code, 401)
42+
43+
def testForbidden(self):
44+
err = exceptions.HttpError.FromResponse(_MakeResponse(403))
45+
self.assertIsInstance(err, exceptions.HttpError)
46+
self.assertIsInstance(err, exceptions.HttpForbiddenError)
47+
self.assertEquals(err.status_code, 403)
48+
49+
def testNotFound(self):
50+
err = exceptions.HttpError.FromResponse(_MakeResponse(404))
51+
self.assertIsInstance(err, exceptions.HttpError)
52+
self.assertIsInstance(err, exceptions.HttpNotFoundError)
53+
self.assertEquals(err.status_code, 404)
54+
55+
def testConflict(self):
56+
err = exceptions.HttpError.FromResponse(_MakeResponse(409))
57+
self.assertIsInstance(err, exceptions.HttpError)
58+
self.assertIsInstance(err, exceptions.HttpConflictError)
59+
self.assertEquals(err.status_code, 409)
60+
61+
def testUnknownStatus(self):
62+
err = exceptions.HttpError.FromResponse(_MakeResponse(499))
63+
self.assertIsInstance(err, exceptions.HttpError)
64+
self.assertEquals(err.status_code, 499)
65+
66+
def testMalformedStatus(self):
67+
err = exceptions.HttpError.FromResponse(_MakeResponse('BAD'))
68+
self.assertIsInstance(err, exceptions.HttpError)

apitools/scripts/oauth2l_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ class _FakeResponse(object):
3535

3636
def __init__(self, status_code, scopes=None):
3737
self.status_code = status_code
38+
self.info = {
39+
'reason': str(http_client.responses[self.status_code]),
40+
'status': str(self.status_code),
41+
}
3842
if self.status_code == http_client.OK:
3943
self.content = json.dumps({'scope': ' '.join(scopes or [])})
4044
else:
4145
self.content = 'Error'
42-
self.info = str(http_client.responses[self.status_code])
4346
self.request_url = 'some-url'
4447

4548

0 commit comments

Comments
 (0)