add some sense of api error handling on web server

This commit is contained in:
Akbar Rahman 2022-12-16 18:43:19 +00:00
parent 4c4a747e16
commit 0e2aef1941
Signed by: alvierahman90
GPG Key ID: 20609519444A1269
2 changed files with 18 additions and 11 deletions

View File

@ -96,6 +96,9 @@ class Db:
'grant_type': 'refresh_token',
'refresh_token': refresh_token,
})
if res.status_code != 200:
return None
print(res.status_code)
print(res.content)
res = res.json()

View File

@ -93,11 +93,15 @@ def index():
'description': f"{count}{counttype}"
})
resp = create_rq.json()
if create_rq.status_code != 201:
return resp
return render_template("index_authorised.html", message={
'type': 'error',
'text': f"unexpected error when creating playlist {playlist_name}"
})
playlist_id = resp.get('id')
db.user_add_playlist(user_id, playlist_id, count, counttype)
@ -135,13 +139,10 @@ def cb():
token_rq = rq.post(f"{SPOTIFY_TOKEN_ENDPOINT}", data = data, headers = headers)
if token_rq.status_code != rq.codes.ok:
resp = make_response(json.dumps({
'status_code': token_rq.status_code,
'resp:': token_rq.json(),
'data': data
}))
resp.headers.set('Content-Type', 'application/json')
return resp
return render_template("index_authorised.html", message={
'type': 'error',
'text': f"unable to authenticate with spotify. please try again later."
})
resp = token_rq.json()
spotify_token = resp['access_token']
@ -149,9 +150,12 @@ def cb():
refresh_token = resp['refresh_token']
me_rq = rq.get(f"{SPOTIFY_API_ENDPOINT}/me", headers = { 'Authorization': f"Bearer {spotify_token}" })
resp = me_rq.json()
if me_rq.status_code != 200:
return resp
return render_template("index_authorised.html", message={
'type': 'error',
'text': f"unable to authenticate with spotify. please try again later."
})
resp = me_rq.json()
spotify_id = resp['id']
db.add_user(user_secret, spotify_token, spotify_id, token_expires, refresh_token, client_secret_expires=USER_SECRET_EXPIRATION)