Skip to content

[FIX] Catch EnumParsingException for unknown platforms in web routes - #1293

Open
kaihere14 wants to merge 1 commit into
CCExtractor:masterfrom
kaihere14:fix/enum-parsing-exception-handlers
Open

kaihere14 wants to merge 1 commit into
CCExtractor:masterfrom
kaihere14:fix/enum-parsing-exception-handlers

Conversation

@kaihere14

Copy link
Copy Markdown

In raising this pull request, I confirm the following (please check boxes):

  • 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 closed at the will of the maintainer.
  • I give this submission freely, and claim no ownership to its content.

My familiarity with the project is as follows (check one):

  • 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.

The problem

Three pages take a platform name in the URL, like linux or windows. If you give a name that doesn't exist, each page is meant to handle it nicely. Instead, all three crash with a 500 server error.

URL Meant to return Returns today
/test/master/invalid 404 page 500 error
/maintenance-mode/invalid ERROR 500 error
/toggle_maintenance/invalid/True (admin) {"status": "failed", "message": "Platform Not found"} 500 error

How to reproduce

Open any of the URLs above with a platform name that isn't linux or windows. You get the 500 error page, and the log shows exceptions.EnumParsingException.

Why it happens

These routes turn the name into a platform with TestPlatform.from_string() and catch ValueError if the name is wrong.

But from_string() in database.py doesn't raise ValueError. Since #329 it raises EnumParsingException, which is defined in exceptions.py and is not a ValueError. So the except ValueError lines never run, and the error crashes the page.

The test that should have caught this didn't. test_in_maintenance_mode_ValueError posted to /maintenance/invalid, which is the wrong URL, and checked the result with assertIsNotNone. That check passes for any response, so the test passed without ever reaching the route.

The fix

Change except ValueError to except EnumParsingException in these three places:

  • latest_commit_info() in mod_test/controllers.py
  • toggle_maintenance() and in_maintenance_mode() in mod_ci/controllers.py

Nothing else changes. A valid platform name works exactly as before.

Tests

  • tests/test_ci/test_controllers.py: the broken test now uses the right URL and checks for ERROR. It's renamed to test_in_maintenance_mode_invalid_platform.
  • tests/test_ci/test_controllers.py: new test_toggle_maintenance_invalid_platform checks the "Platform Not found" response.
  • tests/test_test/test_controllers.py: new test_latest_commit_info_unknown_platform checks for a 404.
  • All three tests fail on master with a 500 and pass with this fix.
  • Full test suite: master 830 tests OK. This branch has 832 tests, all OK: one test was replaced and two were added. No other test result changed.
  • isort, pycodestyle, pydocstyle and mypy are clean on the changed files.

This touches the same file as #1276 (mod_test/controllers.py), but a different line. I checked, and the two merge without conflicts.

TestPlatform.from_string raises EnumParsingException since CCExtractor#329, but
/test/master/<platform>, /maintenance-mode/<platform> and
/toggle_maintenance/<platform>/<status> still caught ValueError, so an
unknown platform returned a 500 instead of the handled response.

The existing test for /maintenance-mode used a wrong URL and
assertIsNotNone, so it passed without reaching the route.
@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.

1 participant