Skip to content

Commit

Permalink
Merge pull request #5 from Codeacious/master
Browse files Browse the repository at this point in the history
Throwing exceptions on 400s and 500s in _do_request
  • Loading branch information
mbabineau committed Aug 19, 2014
2 parents dd8220b + 2a6a2af commit dbe1d7e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
10 changes: 8 additions & 2 deletions marathon/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import marathon
from .models import MarathonApp, MarathonTask, MarathonEndpoint
from .exceptions import NotFoundError
from .exceptions import InternalServerError, NotFoundError


class MarathonClient(object):
Expand Down Expand Up @@ -58,7 +58,13 @@ def _do_request(self, method, path, params=None, data=None):
response = requests.request(method, url, params=params, data=data, headers=headers,
auth=self.auth, timeout=self.timeout)

if response.status_code >= 300:
if response.status_code >= 500:
marathon.log.error("Got HTTP {code}: {body}".format(code=response.status_code, body=response.text))
raise InternalServerError(response)
elif response.status_code >= 400:
marathon.log.error("Got HTTP {code}: {body}".format(code=response.status_code, body=response.text))
raise NotFoundError(response)
elif response.status_code >= 300:
marathon.log.warn("Got HTTP {code}: {body}".format(code=response.status_code, body=response.text))
else:
marathon.log.debug("Got HTTP {code}: {body}".format(code=response.status_code, body=response.text))
Expand Down
11 changes: 7 additions & 4 deletions marathon/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ def __init__(self, response):
"""
content = response.json()
self.status_code = response.status_code
self.status_desc = content['error']['statusdesc']
self.error_message = content['error']['errormessage']
self.error_message = content['message']
super(MarathonHttpError, self).__init__(self.__str__() )

def __repr__(self):
return 'MarathonError: HTTP `%s - %s` returned with message, "%s"' % \
(self.status_code, self.status_desc, self.error_message)
return 'MarathonHttpError: HTTP %s returned with message, "%s"' % \
(self.status_code, self.error_message)

def __str__(self):
return self.__repr__()
Expand All @@ -26,6 +25,10 @@ class NotFoundError(MarathonHttpError):
pass


class InternalServerError(MarathonHttpError):
pass


class InvalidOperatorError(MarathonError):

def __init__(self, operator):
Expand Down

0 comments on commit dbe1d7e

Please sign in to comment.