Update error handling and logging in remindme_caldav.py

- Replace RuntimeError with logging.error.
- Add sys.exit(1) to terminate the program when a critical error occurs.
This commit is contained in:
Sam 2024-02-15 17:48:18 +00:00
parent 30dc31344c
commit ef00f112f3
1 changed files with 6 additions and 3 deletions

View File

@ -30,20 +30,23 @@ def parse_args():
parser.add_argument('--loglevel', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], default='INFO', help='Set the logging level') parser.add_argument('--loglevel', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], default='INFO', help='Set the logging level')
args = parser.parse_args() args = parser.parse_args()
if args.config is None: if args.config is None:
raise RuntimeError("No config file provided. Please use --config path_to_config.toml") logging.error("No config file provided. Please use --config path_to_config.toml")
sys.exit(1)
return args return args
def read_file(filename): def read_file(filename):
try: try:
return Path(filename).read_text() return Path(filename).read_text()
except FileNotFoundError as e: except FileNotFoundError as e:
raise RuntimeError("Error: The specified file does not exist.") logging.error("Error: The specified file does not exist.")
sys.exit(1)
def parse_toml(content): def parse_toml(content):
try: try:
return toml.loads(content) return toml.loads(content)
except Exception as e: except Exception as e:
raise RuntimeError("Error: Failed to parse TOML file.") logging.error("Error: Failed to parse TOML file.")
sys.exit(1)
def calculate_event_hash(event): def calculate_event_hash(event):
return hashlib.md5(json.dumps(event, sort_keys=True, cls=DateTimeEncoder).encode()).hexdigest() return hashlib.md5(json.dumps(event, sort_keys=True, cls=DateTimeEncoder).encode()).hexdigest()