Skip to content

Commit

Permalink
better handle TOML parsing errors
Browse files Browse the repository at this point in the history
Closes #86
  • Loading branch information
fdev31 committed Apr 19, 2024
1 parent 7cf2509 commit a92951c
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions pyprland/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,16 @@ async def __open_config(self, config_filename=""):
self.config.clear()
fname = os.path.expanduser(CONFIG_FILE)

config = self.__load_config_file(fname)
try:
config = self.__load_config_file(fname)
except tomllib.TOMLDecodeError as e:
self.log.critical("Problem reading %s: %s", fname, e)
await notify_error(f"Pyprland failed to read config: {e}")
raise PyprError() from e
except FileNotFoundError as e:
self.log.critical("Unable to open %s: %s", fname, e)
await notify_error(f"Pyprland failed to read config: {e}")
raise PyprError() from e

if not config_filename:
for extra_config in list(config["pyprland"].get("include", [])):
Expand All @@ -87,20 +96,12 @@ def __load_config_file(self, fname):
config = {}
if os.path.exists(fname):
self.log.info("Loading %s", fname)
try:
with open(fname, "rb") as f:
config = tomllib.load(f)
except FileNotFoundError as e:
self.log.critical("Problem reading %s: %s", fname, e)
raise PyprError() from e
with open(fname, "rb") as f:
config = tomllib.load(f)
elif os.path.exists(os.path.expanduser(OLD_CONFIG_FILE)):
self.log.info("Loading %s", OLD_CONFIG_FILE)
try:
with open(os.path.expanduser(OLD_CONFIG_FILE), encoding="utf-8") as f:
config = json.loads(f.read())
except FileNotFoundError as e:
self.log.critical("Problem reading %s: %s", fname, e)
raise PyprError() from e
with open(os.path.expanduser(OLD_CONFIG_FILE), encoding="utf-8") as f:
config = json.loads(f.read())
else:
self.log.critical("Config file not found! Please create %s", fname)
raise PyprError()
Expand Down Expand Up @@ -400,7 +401,7 @@ async def run_client():
manager = Pyprland()

if sys.argv[1] == "version":
print("2.2.8-11") # Automatically updated version
print("2.2.8-12") # Automatically updated version
return

if sys.argv[1] == "edit":
Expand Down

0 comments on commit a92951c

Please sign in to comment.