Skip to content
This repository was archived by the owner on Apr 27, 2025. It is now read-only.

Commit 2a34477

Browse files
committed
Try to improve error handling
1 parent a4c3399 commit 2a34477

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

main.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,20 @@ def __init__(self, cookies, headers):
6161
self.client = httpx.AsyncClient(base_url='https://api.fanbox.cc/', cookies=cookies, headers=headers)
6262
self.client = httpx_caching.CachingClient(self.client)
6363

64-
async def get_payload(self, request):
64+
async def get_payload(self, request, ok_404=False):
6565
response = await self.rate_limiter.limit(request)
66-
if response.status_code == 401:
67-
raise Exception('Fanbox API reports 401 Unauthorized. session_cookies in the config file has likely been invalidated and needs to be updated. Restart the bot after updating.')
68-
if response.status_code == 403:
69-
raise Exception('Fanbox API reports 403 Forbidden. Headers and cookies in the config file likely need to be updated. Restart the bot after updating.')
70-
if response.is_error:
71-
return None
66+
if response.status_code in [401, 403]:
67+
raise Exception(f'Fanbox API reports {response.status_code} {response.reason_phrase}. session_cookies and headers in the config file has likely been invalidated and need to be updated. Restart the bot after updating.')
68+
if response.status_code == 404:
69+
if ok_404:
70+
return None
71+
else:
72+
raise Exception(f'Fanbox API reports {response.status_code} {response.reason_phrase}. This is unexpected for this call.')
73+
response.raise_for_status()
7274
return json.loads(response.text)['body']
7375

7476
async def get_user(self, user_id):
75-
return await self.get_payload(self.client.get('legacy/manage/supporter/user', params={'userId': user_id}))
77+
return await self.get_payload(self.client.get('legacy/manage/supporter/user', params={'userId': user_id}), ok_404=True)
7678

7779
async def get_plans(self):
7880
return await self.get_payload(self.client.get('plan.listCreator', params={'userId': self.self_id}))
@@ -311,6 +313,8 @@ async def fetch_member(discord_id):
311313
return None
312314

313315
def role_from_supporting_plan(user_data):
316+
if user_data is None:
317+
return None
314318
plan = user_data['supportingPlan']
315319
if plan is None:
316320
return None

0 commit comments

Comments
 (0)