diff --git a/.gitignore b/.gitignore index d2d6f36..a9f1863 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,5 @@ nosetests.xml .mr.developer.cfg .project .pydevproject + +*~ \ No newline at end of file diff --git a/buffpy/api.py b/buffpy/api.py index 8ae328f..1506fca 100644 --- a/buffpy/api.py +++ b/buffpy/api.py @@ -9,6 +9,9 @@ 'INFO': 'info/configuration.json' } +class BufferException(Exception): + pass + class API(object): ''' Small and clean class that embrace all basic diff --git a/buffpy/exceptions.py b/buffpy/exceptions.py new file mode 100644 index 0000000..1d46309 --- /dev/null +++ b/buffpy/exceptions.py @@ -0,0 +1,11 @@ +class BufferException(Exception): + def __init__(self, raw_response): + self.code = 0 + if isinstance(raw_response, dict): + self.code = raw_response.get('code',0) + message = raw_response.get('error', 'Unknown Exception') + else: + message = raw_response + + # Call the base class constructor with the parameters it needs + super(BufferException, self).__init__(message) diff --git a/buffpy/managers/profiles.py b/buffpy/managers/profiles.py index d5922db..3441303 100644 --- a/buffpy/managers/profiles.py +++ b/buffpy/managers/profiles.py @@ -1,4 +1,5 @@ from buffpy.models.profile import PATHS, Profile +from buffpy.exceptions import BufferException class Profiles(list): ''' @@ -15,9 +16,13 @@ def __init__(self, api, *args, **kwargs): def all(self): ''' Get all social newtworks profiles + + Handle 401 appropriately ''' response = self.api.get(url=PATHS['GET_PROFILES']) + if 'error' in response and 'code' in response: + raise BufferException(response) for raw_profile in response: self.append(Profile(self.api, raw_profile)) diff --git a/buffpy/managers/updates.py b/buffpy/managers/updates.py index f481f5f..7f47d7b 100644 --- a/buffpy/managers/updates.py +++ b/buffpy/managers/updates.py @@ -1,4 +1,5 @@ from buffpy.models.update import Update +from buffpy.api import BufferException PATHS = { 'GET_PENDING': 'profiles/%s/updates/pending.json', @@ -128,8 +129,14 @@ def new(self, text, shorten=None, now=None, top=None, media=None, when=None): post_data += media_format % (media_type, media_item) response = self.api.post(url=url, data=post_data) - new_update = Update(api=self.api, raw_response=response['updates'][0]) - - self.append(new_update) - - return new_update + + #error handling + success = response.get('success',False) + if success and 'updates' in response: + new_update = Update(api=self.api, raw_response=response['updates'][0]) + self.append(new_update) + return new_update + elif 'message' in response: + raise BufferException(response['message']) + else: + raise BufferException("Unknown Error")