Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions scripts/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,47 @@ def update_readme(
logger.info(
f"Updated README with new image and description for {entry_title}."
)

def open_data_file(file_path):
"""
Open and read a data file with proper error handling.

If a process or report script attempts to open a file that does not exist,
an exception is generated and converted into a helpful error message.

Args:
file_path (str): Path to the data file to open

Returns:
str: Content of the file

Raises:
QuantifyingException: If file doesn't exist or can't be opened
"""
try:
if not os.path.exists(file_path):
raise QuantifyingException(
f"Data file not found: {file_path}",
exit_code=1
)

with open(file_path, 'r', encoding='utf-8') as f:
return f.read()

except QuantifyingException:
raise
Comment on lines +394 to +395
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are redundant and unnecessary. You shouldn't catch an exception to simply raise it again.

except FileNotFoundError as e:
raise QuantifyingException(
f"Data file not found: {file_path}",
exit_code=1
)
except PermissionError as e:
raise QuantifyingException(
f"Permission denied when accessing data file: {file_path}",
exit_code=1
)
except Exception as e:
raise QuantifyingException(
f"Error opening data file {file_path}: {str(e)}",
exit_code=1
)
Comment on lines +406 to +410
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exceptions should only be turned into error messages if they are well understood. Turning a generic Exception into an error message is ill advised.