Skip to content

[FIX] Guard .first() database queries against None to prevent AttributeError - #1276

Open
Aspirant200715 wants to merge 1 commit into
CCExtractor:masterfrom
Aspirant200715:fix/guard-none-queries-attribute-error
Open

Aspirant200715 wants to merge 1 commit into
CCExtractor:masterfrom
Aspirant200715:fix/guard-none-queries-attribute-error

Conversation

@Aspirant200715

Copy link
Copy Markdown

In raising this pull request, I confirm the following:

  • I have read and understood the contributors guide.
  • I have checked that another pull request for this purpose does not exist.
  • I have considered and confirmed that this submission will be valuable to others.
  • I accept that this submission may not be used, and the pull request may be closed at the discretion of the maintainer.
  • I give this submission freely and claim no ownership of its content.

My familiarity with the project is as follows:

  • I have never used the project.
  • I have used the project briefly.
  • I have used the project extensively, but have not contributed previously.
  • I am an active contributor to the project.

Description

Fixes #1275

This pull request prevents crashes caused by unhandled AttributeError exceptions ('NoneType' object has no attribute 'value' / 'commit') on routes querying GeneralData or CCExtractorVersion when running on a fresh installation or an empty/unseeded database.

Root Cause

  1. mod_home/controllers.py

    • The last_commit query unconditionally dereferences .first().value on GeneralData.
    • If the database entry has not been seeded, .first() returns None, resulting in an AttributeError.
  2. mod_sample/controllers.py

    • The latest_commit and last_release queries unconditionally dereference .first().value and .first().commit, respectively.
    • If either database entry is missing, the application crashes with an AttributeError.
    • Additionally, passing None to downstream queries can trigger unnecessary WHERE commit IS NULL database lookups.
  3. mod_test/controllers.py

    • In latest_commit_info, commit_hash unconditionally dereferences .first().value for fetch_commit_<platform>.
    • If the platform-specific commit entry is missing, the endpoint returns an unhandled HTTP 500 error instead of raising TestNotFoundException and rendering the appropriate 404 page.

Solution

  1. mod_home/controllers.py

    • Added a guard for the last_commit query result.
    • Passes None to the template context when the entry is missing, which Jinja2 handles gracefully.
  2. mod_sample/controllers.py

    • Added guards for latest_commit_entry and last_release_entry.
    • Ensured that downstream Test.query.filter() lookups execute only when the corresponding commit hashes exist.
  3. mod_test/controllers.py

    • Added a check for the existence of the platform-specific fetch_commit_<platform> entry in GeneralData.
    • If the entry is missing, logs the error and raises:
    raise TestNotFoundException(
        f"There is no commit hash available for {str(platform)}"
    )
    • This triggers the blueprint error handler to render test/test_not_found.html with an HTTP 404 response instead of an HTTP 500 error.
  4. Unit Tests

    • Added test_root_without_general_data in tests/test_home/test_controllers.py.
    • Added test_sample_info_without_commit_or_release in tests/test_sample/test_controllers.py.
    • Added test_by_platform_no_commit_hash in tests/test_test/test_controllers.py.

Verification

All linters and type checkers pass without issues:

Tool Result
pycodestyle 0 issues
pydocstyle 0 issues
isort 0 issues
mypy No issues found in 3 source files
dodgy 0 warnings

…vent AttributeError

Guard against AttributeError on empty database or unseeded GeneralData and CCExtractorVersion records.

- mod_home/controllers.py: guard last_commit query on index route
    - mod_sample/controllers.py: guard last_commit and last_release queries; avoid querying Test when commit hash is None
    - mod_test/controllers.py: raise TestNotFoundException in by_platform if fetch_commit entry is missing
    - tests/: add unit tests for unseeded database scenarios in test_home, test_sample, and test_test
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Unhandled AttributeError on empty database queries for GeneralData and CCExtractorVersion

1 participant