From 68899a0cc4837f0202e2542f5308e61d079e7a2e Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Thu, 24 Nov 2016 12:03:42 +0100 Subject: Added some error handling to log_in --- mastodon/Mastodon.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 558d1c1..efbe89d 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -95,8 +95,17 @@ class Mastodon: params['grant_type'] = 'password' params['scope'] = " ".join(scopes) - response = self.__api_request('POST', '/oauth/token', params) - self.access_token = response['access_token'] + try: + response = self.__api_request('POST', '/oauth/token', params) + self.access_token = response['access_token'] + except: + raise ValueError('Invalid user name, password or scopes.') + + requested_scopes = " ".join(scopes) + received_scopes = " ".join(sorted(response["scopes"].split(" "))) + + if requested_scopes != received_scopes: + raise ValueError('Granted scopes "' + received_scopes + '" differ from requested scopes "' + requested_scopes + '".') if to_file != None: with open(to_file, 'w') as token_file: @@ -330,7 +339,12 @@ class Mastodon: if response.status_code == 500: raise IOError('General API problem.') - return response.json() + try: + response = response.json() + except: + raise ValueError("Could not parse response as JSON, respose code was " + str(response.status_code)) + + return response def __generate_params(self, params, exclude = []): """ -- cgit v1.2.3