Skip to content

Commit 234e906

Browse files
committed
HTTP exceptions should allow easy access to response data.
Thanks to Stéphane Angel for the suggestion. Refs ask#62.
1 parent 96c2fa7 commit 234e906

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

github2/request.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44
import time
55
import httplib2
6+
from httplib import responses
67
try:
78
import json as simplejson # For Python 2.6
89
except ImportError:
@@ -41,6 +42,25 @@ class GithubError(Exception):
4142
"""An error occured when making a request to the Github API."""
4243

4344

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+
4464
class GithubRequest(object):
4565
github_url = GITHUB_URL
4666
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"):
143163
logging.debug("URL: %r POST_DATA: %r RESPONSE_TEXT: %r", url,
144164
post_data, content)
145165
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)
148169
json = simplejson.loads(content.decode(charset_from_headers(response)))
149170
if json.get("error"):
150171
raise self.GithubError(json["error"][0]["error"])

0 commit comments

Comments
 (0)