Attempt to fix:
JSONDecodeError,
InternalError: Unread result found as per issue #004
This commit is contained in:
GaMeNu 2023-10-15 11:47:45 +03:00
parent 2e289131e8
commit cc2ce8851b
2 changed files with 23 additions and 6 deletions

View File

@ -33,14 +33,17 @@ class AlertReqs:
decoded = req.content.decode('utf-8-sig') decoded = req.content.decode('utf-8-sig')
if decoded is None or len(decoded) < 3: # Why does it get a '\r\n' wtf if decoded is None or len(decoded) < 3: # Why does it get a '\r\n' wtf
ret_dict = None ret_dict = {}
else: else:
try:
ret_dict = json.loads(decoded) ret_dict = json.loads(decoded)
except (json.decoder.JSONDecodeError, json.JSONDecodeError):
ret_dict = None
return ret_dict return ret_dict
@staticmethod @staticmethod
def request_history_json() -> dict: def request_history_json() -> dict | None:
""" """
Request a json of the alert history from last day Request a json of the alert history from last day
:return: JSON object as Python dict :return: JSON object as Python dict
@ -50,7 +53,11 @@ class AlertReqs:
content = req.text content = req.text
return json.loads(content) try:
ret_dict = json.loads(content)
except (json.JSONDecodeError, json.decoder.JSONDecodeError):
ret_dict = None
return ret_dict
class Alert: class Alert:
@ -63,7 +70,10 @@ class Alert:
@staticmethod @staticmethod
def from_dict(data: dict): def from_dict(data: dict):
return Alert(int(data.get('id', '0')), int(data.get('cat', '0')), data.get('title'), data.get('data'), return Alert(int(data.get('id', '0')),
int(data.get('cat', '0')),
data.get('title'),
data.get('data'),
data.get('desc')) data.get('desc'))
@ -108,7 +118,9 @@ class Notificator(commands.Cog):
return return
self.log.debug(f'Alert response: {current_alert}') self.log.debug(f'Alert response: {current_alert}')
if current_alert is None or len(current_alert) == 0:
if current_alert is None: if current_alert is None:
self.log.warning('Error while current alert data.')
if len(self.active_districts) == 0: if len(self.active_districts) == 0:
return return
@ -152,6 +164,7 @@ class Notificator(commands.Cog):
@staticmethod @staticmethod
def generate_alert_embed(alert_object: Alert, district: str, arrival_time: int | None, time: str, def generate_alert_embed(alert_object: Alert, district: str, arrival_time: int | None, time: str,
lang: str) -> discord.Embed: lang: str) -> discord.Embed:
# TODO: Using 1 generate alert function is probably bad, should probably split into a utility class
e = discord.Embed(color=discord.Color.from_str('#FF0000')) e = discord.Embed(color=discord.Color.from_str('#FF0000'))
e.title = f'התראה ב{district}' e.title = f'התראה ב{district}'
e.add_field(name=district, value=alert_object.title, inline=False) e.add_field(name=district, value=alert_object.title, inline=False)

View File

@ -93,6 +93,7 @@ class DBAccess:
with self.connection.cursor() as crsr: with self.connection.cursor() as crsr:
crsr.execute('SELECT * FROM areas WHERE area_id=%s', (id,)) crsr.execute('SELECT * FROM areas WHERE area_id=%s', (id,))
res = crsr.fetchone() res = crsr.fetchone()
crsr.fetchall()
if res is not None: if res is not None:
return Area(res[0], res[1]) return Area(res[0], res[1])
@ -103,6 +104,7 @@ class DBAccess:
with self.connection.cursor() as crsr: with self.connection.cursor() as crsr:
crsr.execute('SELECT * FROM districts WHERE district_id=%s', (id,)) crsr.execute('SELECT * FROM districts WHERE district_id=%s', (id,))
res = crsr.fetchone() res = crsr.fetchone()
crsr.fetchall()
if res is not None: if res is not None:
return District(res[0], res[1], res[2], res[3]) return District(res[0], res[1], res[2], res[3])
@ -116,6 +118,7 @@ class DBAccess:
with self.connection.cursor() as crsr: with self.connection.cursor() as crsr:
crsr.execute('SELECT * FROM servers WHERE server_id=%s', (id,)) crsr.execute('SELECT * FROM servers WHERE server_id=%s', (id,))
res = crsr.fetchone() res = crsr.fetchone()
crsr.fetchall()
if res is not None: if res is not None:
return Server(res[0], res[1]) return Server(res[0], res[1])
@ -126,6 +129,7 @@ class DBAccess:
with self.connection.cursor() as crsr: with self.connection.cursor() as crsr:
crsr.execute('SELECT * FROM channels WHERE channel_id=%s', (id,)) crsr.execute('SELECT * FROM channels WHERE channel_id=%s', (id,))
res = crsr.fetchone() res = crsr.fetchone()
crsr.fetchall()
if res is not None: if res is not None:
return Channel(res[0], res[1], res[2]) return Channel(res[0], res[1], res[2])