Skip to content

Commit

Permalink
Handle JSONDecodeErrors including empty files
Browse files Browse the repository at this point in the history
  • Loading branch information
TSRBerry committed Sep 12, 2023
1 parent c0582ed commit 7eb6bd6
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 14 deletions.
16 changes: 15 additions & 1 deletion robocop_ng/helpers/disabled_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
from typing import Union

from robocop_ng.helpers.notifications import report_critical_error


def get_disabled_ids_path(bot) -> str:
return os.path.join(bot.state_dir, "data/disabled_ids.json")
Expand All @@ -22,7 +24,19 @@ def is_ro_section_valid(ro_section: dict[str, str]) -> bool:
def get_disabled_ids(bot) -> dict[str, dict[str, Union[str, dict[str, str]]]]:
if os.path.isfile(get_disabled_ids_path(bot)):
with open(get_disabled_ids_path(bot), "r") as f:
disabled_ids = json.load(f)
try:
disabled_ids = json.load(f)
except json.JSONDecodeError as e:
content = f.read()
report_critical_error(
bot,
e,
additional_info={
"file": {"length": len(content), "content": content}
},
)
return {}

# Migration code
if "app_id" in disabled_ids.keys():
old_disabled_ids = disabled_ids.copy()
Expand Down
15 changes: 14 additions & 1 deletion robocop_ng/helpers/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
from typing import Optional, Union

from robocop_ng.helpers.notifications import report_critical_error


def get_macros_path(bot):
return os.path.join(bot.state_dir, "data/macros.json")
Expand All @@ -10,7 +12,18 @@ def get_macros_path(bot):
def get_macros_dict(bot) -> dict[str, dict[str, Union[list[str], str]]]:
if os.path.isfile(get_macros_path(bot)):
with open(get_macros_path(bot), "r") as f:
macros = json.load(f)
try:
macros = json.load(f)
except json.JSONDecodeError as e:
content = f.read()
report_critical_error(
bot,
e,
additional_info={
"file": {"length": len(content), "content": content}
},
)
return {}

# Migration code
if "aliases" not in macros.keys():
Expand Down
27 changes: 20 additions & 7 deletions robocop_ng/helpers/restrictions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import json
import os

from robocop_ng.helpers.notifications import report_critical_error


def get_restrictions_path(bot):
return os.path.join(bot.state_dir, "data/restrictions.json")


def get_restrictions(bot):
with open(get_restrictions_path(bot), "r") as f:
return json.load(f)
if os.path.isfile(get_restrictions_path(bot)):
with open(get_restrictions_path(bot), "r") as f:
try:
return json.load(f)
except json.JSONDecodeError as e:
content = f.read()
report_critical_error(
bot,
e,
additional_info={
"file": {"length": len(content), "content": content}
},
)
return {}


def set_restrictions(bot, contents):
Expand All @@ -18,11 +32,10 @@ def set_restrictions(bot, contents):

def get_user_restrictions(bot, uid):
uid = str(uid)
with open(get_restrictions_path(bot), "r") as f:
rsts = json.load(f)
if uid in rsts:
return rsts[uid]
return []
rsts = get_restrictions(bot)
if uid in rsts:
return rsts[uid]
return []


def add_restriction(bot, uid, rst):
Expand Down
18 changes: 16 additions & 2 deletions robocop_ng/helpers/robocronp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,28 @@
import math
import os

from robocop_ng.helpers.notifications import report_critical_error


def get_crontab_path(bot):
return os.path.join(bot.state_dir, "data/robocronptab.json")


def get_crontab(bot):
with open(get_crontab_path(bot), "r") as f:
return json.load(f)
if os.path.isfile(get_crontab_path(bot)):
with open(get_crontab_path(bot), "r") as f:
try:
return json.load(f)
except json.JSONDecodeError as e:
content = f.read()
report_critical_error(
bot,
e,
additional_info={
"file": {"length": len(content), "content": content}
},
)
return {}


def set_crontab(bot, contents):
Expand Down
14 changes: 13 additions & 1 deletion robocop_ng/helpers/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os.path
import os

from robocop_ng.helpers.notifications import report_critical_error


def get_persistent_roles_path(bot):
return os.path.join(bot.state_dir, "data/persistent_roles.json")
Expand All @@ -10,7 +12,17 @@ def get_persistent_roles_path(bot):
def get_persistent_roles(bot) -> dict[str, list[str]]:
if os.path.isfile(get_persistent_roles_path(bot)):
with open(get_persistent_roles_path(bot), "r") as f:
return json.load(f)
try:
return json.load(f)
except json.JSONDecodeError as e:
content = f.read()
report_critical_error(
bot,
e,
additional_info={
"file": {"length": len(content), "content": content}
},
)
return {}


Expand Down
18 changes: 16 additions & 2 deletions robocop_ng/helpers/userlogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
import time

from robocop_ng.helpers.notifications import report_critical_error

userlog_event_types = {
"warns": "Warn",
"bans": "Ban",
Expand All @@ -16,8 +18,20 @@ def get_userlog_path(bot):


def get_userlog(bot):
with open(get_userlog_path(bot), "r") as f:
return json.load(f)
if os.path.isfile(get_userlog_path(bot)):
with open(get_userlog_path(bot), "r") as f:
try:
return json.load(f)
except json.JSONDecodeError as e:
content = f.read()
report_critical_error(
bot,
e,
additional_info={
"file": {"length": len(content), "content": content}
},
)
return {}


def set_userlog(bot, contents):
Expand Down

0 comments on commit 7eb6bd6

Please sign in to comment.