From 0546c2e7bf5940e52508dbb6a73e5df67a8dacca Mon Sep 17 00:00:00 2001 From: Andrew Williams Date: Mon, 18 Oct 2010 17:25:48 +0100 Subject: [PATCH] Add some further error checking to the Reddit API module --- reddit/api.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/reddit/api.py b/reddit/api.py index 6f6fc13..3ce84b1 100644 --- a/reddit/api.py +++ b/reddit/api.py @@ -7,6 +7,9 @@ import unicodedata class NotLoggedIn(Exception): pass +class LoginError(Exception): + pass + class Comment(dict): """ Abstraction for comment data provided by JSON Comments can be identifed by Kind = 1 """ @@ -81,11 +84,15 @@ class Inbox(): jsondoc = json.load(self._url_request(url, data)) - self.login_cookie = jsondoc['json']['data']['cookie'] - self.modhash = jsondoc['json']['data']['modhash'] + if jsondoc and 'json' in jsondoc: + if 'data' in jsondoc['json']: + self.login_cookie = jsondoc['json']['data']['cookie'] + self.modhash = jsondoc['json']['data']['modhash'] + if self.login_cookie: + return True + elif 'errors' in jsondoc['json']: + raise LoginError(jsondoc['json']['errors']) - if self.login_cookie: - return True return False @property