From 03cc06d79cc20ec6f428d7c8afe4aca0c509c3f0 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 13 Aug 2016 19:40:27 +0200 Subject: [PATCH] Handle "player not found" errors correctly --- overwatch.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/overwatch.py b/overwatch.py index df50ef25..c960541e 100644 --- a/overwatch.py +++ b/overwatch.py @@ -2,6 +2,10 @@ import asyncio import requests loop = asyncio.get_event_loop() + +class NotFoundException(Exception): + pass + # Get player data async def get_player_data(platform: str, region: str, battletag: str, **kwargs): print("[Overwatch] Getting player info for: {platform} {region} {battletag}".format(platform=platform, @@ -14,9 +18,16 @@ async def get_player_data(platform: str, region: str, battletag: str, **kwargs): 'https://api.lootbox.eu/{platform}/{region}/{battletag}/profile'.format(**locals())) # Ensure the request is successful if r.status_code == 200: - return r.json() - elif r.status_code == 404: - raise Exception("Player not found.") + # Parse json and check for the status code inside the response + pj = r.json() + if "statusCode" in pj: + if pj["statusCode"] == 404: + raise NotFoundException("Player not found.") + else: + raise Exception("Unhandled API response.") + else: + # Success! + return pj else: raise Exception("Unhandled API response.") \ No newline at end of file