diff options
author | Tim Burke <tim.burke@gmail.com> | 2016-01-18 17:05:28 -0800 |
---|---|---|
committer | Tim Burke <tim.burke@gmail.com> | 2016-03-03 17:16:33 +0000 |
commit | 9b8ab67a780416508b995adafb07e96ea646d6f8 (patch) | |
tree | e8b1b580a0e2e2d9dc941924f408966da4ffdfe5 /swiftclient/exceptions.py | |
parent | cd3a4dbf0adce0b7a6779755caaf36a0e983e5fb (diff) | |
download | python-swiftclient-9b8ab67a780416508b995adafb07e96ea646d6f8.tar.gz |
Include response headers in ClientExceptions
Now, client applications can get to things like transaction IDs for
failures without needing to turn on all of logging.
While we're at it, add a from_response factory method for
ClientException.
Co-Authored-By: Alexander Corwin <ancorwin@gmail.com>
Change-Id: Ib46d5f8fc7f36f651f5908bb9d900316fdaebce3
Diffstat (limited to 'swiftclient/exceptions.py')
-rw-r--r-- | swiftclient/exceptions.py | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/swiftclient/exceptions.py b/swiftclient/exceptions.py index 370a8d0..da70379 100644 --- a/swiftclient/exceptions.py +++ b/swiftclient/exceptions.py @@ -13,12 +13,15 @@ # See the License for the specific language governing permissions and # limitations under the License. +from six.moves import urllib + class ClientException(Exception): def __init__(self, msg, http_scheme='', http_host='', http_port='', http_path='', http_query='', http_status=None, http_reason='', - http_device='', http_response_content=''): + http_device='', http_response_content='', + http_response_headers=None): super(ClientException, self).__init__(msg) self.msg = msg self.http_scheme = http_scheme @@ -30,6 +33,16 @@ class ClientException(Exception): self.http_reason = http_reason self.http_device = http_device self.http_response_content = http_response_content + self.http_response_headers = http_response_headers + + @classmethod + def from_response(cls, resp, msg=None, body=None): + msg = msg or '%s %s' % (resp.status_code, resp.reason) + body = body or resp.content + parsed_url = urllib.parse.urlparse(resp.request.url) + return cls(msg, parsed_url.scheme, parsed_url.hostname, + parsed_url.port, parsed_url.path, parsed_url.query, + resp.status_code, resp.reason, '', body, resp.headers) def __str__(self): a = self.msg |