From ea26754c843922131b44ec692eda43c9f6db063a Mon Sep 17 00:00:00 2001 From: Anthony Cui Date: Wed, 20 Dec 2023 22:56:11 -0500 Subject: [PATCH] Update exception handling to catch more common errors --- ersilia/core/tracking.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/ersilia/core/tracking.py b/ersilia/core/tracking.py index 0e4600748..15f27e353 100644 --- a/ersilia/core/tracking.py +++ b/ersilia/core/tracking.py @@ -5,7 +5,7 @@ import tempfile import logging import boto3 -from botocore.exceptions import ClientError +from botocore.exceptions import ClientError, NoCredentialsError import os PERSISTENT_FILE_PATH = os.path.abspath("current_session.txt") @@ -17,15 +17,18 @@ def log_files_metrics(file): error_count = 0 warning_count = 0 - with open(file, "r") as file: - for line in file: - if "| ERROR" in line: - error_count += 1 - elif "| WARNING" in line: - warning_count += 1 + try: + with open(file, "r") as file: + for line in file: + if "| ERROR" in line: + error_count += 1 + elif "| WARNING" in line: + warning_count += 1 - write_persistent_file(f"Error count: {error_count}") - write_persistent_file(f"Warning count: {warning_count}") + write_persistent_file(f"Error count: {error_count}") + write_persistent_file(f"Warning count: {warning_count}") + except FileNotFoundError: + logging.warning("Log file not found") def read_csv(file): @@ -89,6 +92,8 @@ def upload_to_s3(json_dict, bucket="t4sg-ersilia", object_name=None): s3_client = boto3.client("s3") try: s3_client.upload_file(tmp.name, bucket, f"{object_name}.json") + except NoCredentialsError: + logging.error("Unable to upload tracking data to AWS: Credentials not found") except ClientError as e: logging.error(e) return False