Skip to content

Commit 76cec43

Browse files
committed
More error handling
1 parent f5c86d1 commit 76cec43

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

shared/http_requests.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,26 @@ def __init__(self, session: aiohttp.ClientSession):
1010
async def post_request(self, category: str, topic: str):
1111
async with self.semaphore:
1212
async with self.session.post(f"{self.api_url}/{category}", json={"query": [topic]}) as resp:
13-
return await resp.json()
13+
if resp.status != 200:
14+
return None
15+
else:
16+
return await resp.json()
1417

1518
async def post_request_batch(self, category: str, topics: list[str]):
1619
async with self.semaphore:
1720
async with self.session.post(f"{self.api_url}/{category}", json={"query": topics}) as resp:
18-
return await resp.json()
21+
if resp.status != 200:
22+
return None
23+
else:
24+
return await resp.json()
1925

2026
async def get_request(self, category: str):
2127
async with self.semaphore:
2228
async with self.session.get(f"{self.api_url}/{category}") as resp:
23-
return await resp.json()
29+
if resp.status != 200:
30+
return None
31+
else:
32+
return await resp.json()
2433

2534
async def map_tile_request(self, x: int, z: int):
2635
async with self.semaphore:

watcher/jobs/objective.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ async def check_sessions(requester, tracker):
5454
async with in_transaction():
5555
try:
5656
online_players = await requester.get_request("online")
57+
if not online_players:
58+
print("Online players couldn't be fetched")
59+
return
5760
online_uuids = {player["uuid"] for player in online_players["players"]}
5861

5962
new_players = online_uuids - set(tracker.sessions)
@@ -113,7 +116,11 @@ async def check_sessions(requester, tracker):
113116
session_deletions = []
114117

115118
for player in lost_players:
116-
session = tracker.sessions.pop(player)
119+
# TODO: Check why Keys might be missing here
120+
try:
121+
session = tracker.sessions.pop(player)
122+
except KeyError:
123+
continue
117124
player_uuid, start_date, total_time, positions = session.end_data()
118125

119126
if session.active_obj and session.active_obj.player is not None:
@@ -215,6 +222,9 @@ async def check_town_blocks(requester):
215222
try:
216223
print("Checking townblocks...")
217224
towns = await requester.get_request("towns")
225+
if not towns:
226+
print("Towns couldn't be fetched")
227+
return
218228

219229
async def update_town_blocks(town_list: list):
220230
town_data, _ = await get_valid_data(requester, "towns", [town["uuid"] for town in town_list])
@@ -245,6 +255,9 @@ async def update_town_blocks(town_list: list):
245255
async def clean_dead_sessions(requester):
246256
print("Doing some spring cleaning...")
247257
online_players = await requester.get_request("online")
258+
if not online_players:
259+
print("Online players couldn't be fetched")
260+
return
248261
deleted_sessions = 0
249262
for active in await Active.all():
250263
if active.player not in {player["uuid"] for player in online_players["players"]}:

0 commit comments

Comments
 (0)