|
3 | 3 | import re |
4 | 4 | import time |
5 | 5 | import httplib2 |
| 6 | +from httplib import responses |
6 | 7 | try: |
7 | 8 | import json as simplejson # For Python 2.6 |
8 | 9 | except ImportError: |
@@ -41,6 +42,25 @@ class GithubError(Exception): |
41 | 42 | """An error occured when making a request to the Github API.""" |
42 | 43 |
|
43 | 44 |
|
| 45 | +class HttpError(RuntimeError): |
| 46 | + """A HTTP error occured when making a request to the Github API.""" |
| 47 | + def __init__(self, message, content, code): |
| 48 | + """Create a HttpError exception |
| 49 | +
|
| 50 | + :param str message: Exception string |
| 51 | + :param str content: Full content of HTTP request |
| 52 | + :param int code: HTTP status code |
| 53 | + """ |
| 54 | + self.args = (message, content, code) |
| 55 | + self.message = message |
| 56 | + self.content = content |
| 57 | + self.code = code |
| 58 | + if code: |
| 59 | + self.code_reason = responses[code] |
| 60 | + else: |
| 61 | + self.code_reason = "" |
| 62 | + |
| 63 | + |
44 | 64 | class GithubRequest(object): |
45 | 65 | github_url = GITHUB_URL |
46 | 66 | url_format = "%(github_url)s/api/%(api_version)s/%(api_format)s" |
@@ -143,8 +163,9 @@ def raw_request(self, url, extra_post_data, method="GET"): |
143 | 163 | logging.debug("URL: %r POST_DATA: %r RESPONSE_TEXT: %r", url, |
144 | 164 | post_data, content) |
145 | 165 | if response.status >= 400: |
146 | | - raise RuntimeError("unexpected response from github.com %d: %r" % ( |
147 | | - response.status, content)) |
| 166 | + raise HttpError("Unexpected response from github.com %d: %r" |
| 167 | + % (response.status, content), content, |
| 168 | + response.status) |
148 | 169 | json = simplejson.loads(content.decode(charset_from_headers(response))) |
149 | 170 | if json.get("error"): |
150 | 171 | raise self.GithubError(json["error"][0]["error"]) |
|
0 commit comments