8000 Handle exceptions raised from Requests · goodwillcoding/github3.py@3217bbb · GitHub
[go: up one dir, main page]

Skip to content

Commit 3217bbb

Browse files
committed
Handle exceptions raised from Requests
This particular approach allows us to avoid having to rewrite every unit test that makes an assertion about the mocked session having its get, post, patch, etc. methods called. It is sub-optimal, but until we have the time and patience to rewrite the tests, thi swill have to make do. Closes sigmavirus24#597
1 parent 94c1530 commit 3217bbb

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

github3/models.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from datetime import datetime
1414
from logging import getLogger
1515

16+
import requests
17+
1618
from . import exceptions
1719
from .decorators import requires_auth
1820
from .null import NullObject
@@ -176,27 +178,38 @@ def _boolean(self, response, true_code, false_code):
176178
raise exceptions.error_for(response)
177179
return False
178180

181+
def _request(self, method, *args, **kwargs):
182+
try:
183+
request_method = getattr(self.session, method)
184+
return request_method(*args, **kwargs)
185+
except (requests.exceptions.ConnectionError,
186+
requests.exceptions.Timeout,
187+
) as exc:
188+
raise exceptions.ConnectionError(exc)
189+
except requests.exceptions.RequestException as exc:
190+
raise exceptions.TransportError(exc)
191+
179192
def _delete(self, url, **kwargs):
180193
__logs__.debug('DELETE %s with %s', url, kwargs)
181-
return self.session.delete(url, **kwargs)
194+
return self._request('delete', url, **kwargs)
182195

183196
def _get(self, url, **kwargs):
184197
__logs__.debug('GET %s with %s', url, kwargs)
185-
return self.session.get(url, **kwargs)
198+
return self._request('get', url, **kwargs)
186199

187200
def _patch(self, url, **kwargs):
188201
__logs__.debug('PATCH %s with %s', url, kwargs)
189-
return self.session.patch(url, **kwargs)
202+
return self._request('patch', url, **kwargs)
190203

191204
def _post(self, url, data=None, json=True, **kwargs):
192205
if json:
193206
data = dumps(data) if data is not None else data
194207
__logs__.debug('POST %s with %s, %s', url, data, kwargs)
195-
return self.session.post(url, data, **kwargs)
208+
return self._request('post', url, data, **kwargs)
196209

197210
def _put(self, url, **kwargs):
198211
__logs__.debug('PUT %s with %s', url, kwargs)
199-
return self.session.put(url, **kwargs)
212+
return self._request('put', url, **kwargs)
200213

201214
def _build_url(self, *args, **kwargs):
202215
"""Builds a new API url from scratch."""

0 commit comments

Comments
 (0)
0