Skip to content

Commit

Permalink
[api] Throw error object if ok == 0
Browse files Browse the repository at this point in the history
If "ok" is 0, throw an exception instead of returning the decoded
json.  The exception it returns stringifies nicely by default and
exposes the response object as an attribute (.response) on the exception
instance.  You can also access the details of the decoded json by
examining the .content attribute
  • Loading branch information
fmoo committed Aug 10, 2011
1 parent ad2a81c commit 212ee2c
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions glitch/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
from compat import json


class GlitchError(ValueError):
""" Wrapper for errors to be thrown if content['ok'] is 0 """
def __init__(self, content, response):
self.content = content
self.response = response
super(GlitchError, self).__init__(self.error)

@property
def error(self):
return self.content['error']


class GlitchAPI(object):
DOMAIN = 'api.glitch.com'
BASE_PATH = '/simple'
Expand Down Expand Up @@ -38,8 +50,13 @@ def _postprocess(self, response):
response.raise_for_status()

# For some reason, this doesn't work.
#return json.load(response)
return json.loads(response.content)
#content = json.load(response)
content = json.loads(response.content)

if content['ok'] == '0' or content['ok'] == 0:
raise GlitchError(content, response)

return content

def _uri(self, path):
"""
Expand Down

0 comments on commit 212ee2c

Please sign in to comment.