Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from boydgreenfield/enhanced_status_messages
Browse files Browse the repository at this point in the history
Annotate HTTPError messages with Potion exceptions
  • Loading branch information
lyschoening authored Jun 22, 2016
2 parents d918e7a + 51e9948 commit ba79269
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion potion_client/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import re

from requests import Request
from requests.exceptions import HTTPError

from potion_client import PotionJSONDecoder
from potion_client.collection import PaginatedList
Expand Down Expand Up @@ -66,14 +67,33 @@ def request_factory(self, data, params):
data=json.dumps(request_data, cls=PotionJSONEncoder))
return req

def raise_for_status(self, response):
http_error_msg = ''

if 400 <= response.status_code < 500:
try:
http_error_msg = response.json()
except:
http_error_msg = ('{code} Client Error: {reason} for url: {url}'.format(
code=response.status_code, reason=response.reason, url=response.url)
)

elif 500 <= response.status_code < 600:
http_error_msg = ('{code} Server Error: {reason} for url: {url}'.format(
code=response.status_code, reason=response.reason, url=response.url)
)

if http_error_msg:
raise HTTPError(http_error_msg, response=response)

def make_request(self, data, params):
req = self.request_factory(data, params)
prepared_request = self.owner._client.session.prepare_request(req)

response = self.owner._client.session.send(prepared_request)

# return error for some error conditions
response.raise_for_status()
self.raise_for_status(response)

return response, response.json(cls=PotionJSONDecoder,
client=self.owner._client,
Expand Down

0 comments on commit ba79269

Please sign in to comment.