Skip to content

Conversation

@harsha08-2k6
Copy link

@harsha08-2k6 harsha08-2k6 commented Nov 24, 2025

Fixes

Description

This PR implements the open_data_file() function in the shared library (scripts/shared.py) to handle file operations with proper error handling. The function catches anticipated exceptions and converts them into helpful error messages using QuantifyingException, preventing raw exceptions from being displayed when process or report scripts attempt to open non-existent files.

Technical details

  • Added open_data_file(file_path) function to scripts/shared.py
  • Function checks if file exists before attempting to open it
  • Catches and handles multiple exception types:
    • FileNotFoundError: When file doesn't exist
    • PermissionError: When file cannot be accessed due to permissions
    • Generic Exception: For other unexpected errors
  • All exceptions are converted to QuantifyingException with descriptive error messages and exit code 1
  • Opens files with UTF-8 encoding for proper text handling
  • Returns file content as a string

Tests

Manually tested the function locally by:

  • Testing with non-existent file paths (raises appropriate QuantifyingException)
  • Testing with valid file paths (successfully reads and returns content)
  • Verified error messages are clear and helpful

No automated tests added as this follows the pattern of other utility functions in the shared library.

Screenshots

N/A - Backend function with no UI changes

Checklist

  • I have read and understood the Developer Certificate of Origin (DCO), below, which covers the contents of this pull request (PR).
  • My pull request doesn't include code or content generated with AI.
  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the default branch of the repository (main or master).
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

…ata_file() function to shared library - Issue creativecommons#251

Added a function to open and read a data file with error handling.
@harsha08-2k6 harsha08-2k6 requested review from a team as code owners November 24, 2025 06:32
@harsha08-2k6 harsha08-2k6 requested review from Shafiya-Heena and TimidRobot and removed request for a team November 24, 2025 06:32
@cc-open-source-bot cc-open-source-bot moved this to In review in TimidRobot Nov 24, 2025
@TimidRobot
Copy link
Member

@harsha08-2k6 harsha08-2k6PRs with incomplete descriptions or that don't otherwise follow the included directions can't be accepted.

@TimidRobot TimidRobot self-assigned this Nov 24, 2025
Comment on lines 389 to 405
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
except FileNotFoundError as e:
raise QuantifyingException(
f"Data file not found: {file_path}",
exit_code=1
)
Copy link
Member

Choose a reason for hiding this comment

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

The duplicate file not found logic is nonsensical.

This appears to be AI slop.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the feedback! You're absolutely right - the duplicate file existence check is redundant. I apologize for the oversight.

The os.path.exists() check makes the FileNotFoundError exception handler unreachable. I'll simplify this by removing the pre-check and letting Python's built-in exception handling work naturally:

try:
    with open(file_path, 'r', encoding='utf-8') as f:
        return f.read()
except FileNotFoundError:
    raise QuantifyingException(
        f"Data file not found: {file_path}",
        exit_code=1
    )
except QuantifyingException:
    raise

I'll push this fix shortly. Thanks for catching this!

Removed redundant os.path.exists() check before file open.
The FileNotFoundError exception handler already covers this case,
making the explicit check unnecessary.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

Add shared function to open data files and handle errors

2 participants