From 55c1b225e22a5ce2da1c405fee99ea4315541962 Mon Sep 17 00:00:00 2001 From: Philip Paquette Date: Fri, 6 Sep 2019 19:02:04 -0400 Subject: [API] Catching TypeError and ValueError on json.loads --- diplomacy/integration/webdiplomacy_net/api.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'diplomacy') diff --git a/diplomacy/integration/webdiplomacy_net/api.py b/diplomacy/integration/webdiplomacy_net/api.py index 8833b83..b4bde01 100644 --- a/diplomacy/integration/webdiplomacy_net/api.py +++ b/diplomacy/integration/webdiplomacy_net/api.py @@ -55,7 +55,11 @@ class API(BaseAPI): # 200 - Response OK if response.code == 200 and response.body: - list_games_players = json.loads(response.body.decode('utf-8')) + try: + list_games_players = json.loads(response.body.decode('utf-8')) + except (TypeError, ValueError): + LOGGER.warning('ERROR during "%s". Unable to load JSON: %s.', route, response.body.decode('utf-8')) + return return_val for game_player in list_games_players: return_val += [GameIdCountryId(game_id=game_player['gameID'], country_id=game_player['countryID'])] @@ -84,7 +88,11 @@ class API(BaseAPI): # 200 - Response OK if response.code == 200 and response.body: - list_games_players = json.loads(response.body.decode('utf-8')) + try: + list_games_players = json.loads(response.body.decode('utf-8')) + except (TypeError, ValueError): + LOGGER.warning('ERROR during "%s". Unable to load JSON: %s.', route, response.body.decode('utf-8')) + return return_val for game_player in list_games_players: return_val += [GameIdCountryId(game_id=game_player['gameID'], country_id=game_player['countryID'])] @@ -118,7 +126,11 @@ class API(BaseAPI): # 200 - Response OK if response.code == 200 and response.body: - state_dict = json.loads(response.body.decode('utf-8')) + try: + state_dict = json.loads(response.body.decode('utf-8')) + except (TypeError, ValueError): + LOGGER.warning('ERROR during "%s". Unable to load JSON: %s.', route, response.body.decode('utf-8')) + return return_val game, power_name = state_dict_to_game_and_power(state_dict, country_id, max_phases=max_phases) return_val = game, power_name @@ -201,7 +213,11 @@ class API(BaseAPI): return False # Otherwise, validating that received orders are the same as submitted orders - response_body = json.loads(response.body) + try: + response_body = json.loads(response.body.decode('utf-8')) + except (TypeError, ValueError): + LOGGER.warning('ERROR during "%s". Unable to load JSON: %s.', route, response.body.decode('utf-8')) + return False orders_dict = [Order(order, map_name=game.map_name, phase_type=game.phase_type) for order in response_body] all_orders_set = True -- cgit v1.2.3