Skip to content

Commit

Permalink
Added some error handling to log_in
Browse files Browse the repository at this point in the history
  • Loading branch information
halcy committed Nov 24, 2016
1 parent 3a92948 commit 68899a0
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions mastodon/Mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,17 @@ def log_in(self, username, password, scopes = ['read', 'write', 'follow'], to_fi
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:
Expand Down Expand Up @@ -330,7 +339,12 @@ def __api_request(self, method, endpoint, params = {}, files = {}):
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 = []):
"""
Expand Down

0 comments on commit 68899a0

Please sign in to comment.