DevPath icon indicating copy to clipboard operation
DevPath copied to clipboard

[Bug]: Unhandled ValueError in Recommendation Engine Causes 500 API Crash

Open Vinayak051 opened this issue 6 months ago • 8 comments

What happened?

The /api/recommend endpoint doesn't strictly validate the time parameter against its allowed values. If an unrecognized value is passed in the JSON payload, the backend attempts to look it up in a list using .index(), which throws an unhandled ValueError.

To make matters worse, because the app has a global 500 error handler that returns an HTML template (500.html), the API returns HTML instead of JSON. This violates the API contract and will instantly crash frontend JSON parsers expecting a structured response.

Steps to reproduce

  1. Start the Flask development server locally.

  2. Open a terminal and send a POST request with an invalid time value:

curl -X POST http://localhost:5000/api/recommend
-H "Content-Type: application/json"
-d '{"skills": "python", "level": "Beginner", "interest": "Web", "time": "unknown_value"}'

  1. Check the response—instead of a standard JSON error, you get a 500 status code containing the raw HTML of the 500.html error page.

Expected behaviour

The application should validate the input in validate_recommendation_inputs() and gracefully return a 400 Bad Request with a JSON payload, such as: {"error": "Please select a valid time availability."}

Area of the app affected

Recommendation results

Python version

3.11

Operating system

Windows 11

Relevant error output or logs

ValueError: 'unknown_value' is not in list
  File "utils/recommender.py", in score_single_project
    time_availability_index = TIME_AVAILABILITY.index(time_availability.strip().lower())

Before submitting

  • [x] I searched existing issues and this has not been reported before.
  • [x] I can reproduce this bug consistently with the steps above.
  • [x] I am running the latest version of the main branch.

Vinayak051 avatar May 30 '26 08:05 Vinayak051

Thank you for opening your first issue in DevPath.

Please make sure to:

  • Read CONTRIBUTING.md
  • Wait for assignment before starting work
  • Follow repository guidelines carefully

Happy contributing.

github-actions[bot] avatar May 30 '26 08:05 github-actions[bot]

Hi @komalharshita, I would love to work on this issue for GSSoC! Please assign it to me if it looks good to go.

Vinayak051 avatar May 30 '26 08:05 Vinayak051

Hi 👋

I would like to work on this issue under GSSoC'26.

I have previously contributed to open-source projects and have 2 PRs successfully merged. I have gone through the issue requirements and would be happy to work on the implementation while following the project's guidelines and maintaining code quality.

Please assign this issue to me if it is available.

Thank you!

sakshiwankhade026-coder avatar May 30 '26 16:05 sakshiwankhade026-coder

/assign

CodesByY22 avatar Jun 02 '26 06:06 CodesByY22

/assign

Ishita-rxi avatar Jun 26 '26 08:06 Ishita-rxi

I'd like to work on this issue.

From the issue description, the root cause appears to be that the time field is not fully validated before it is used in TIME_AVAILABILITY.index(). When an invalid value is provided, a ValueError is raised, which is caught by the global 500 error handler, causing the API to return an HTML error page instead of the expected JSON response.

My approach will be to:

  • Update validate_recommendation_inputs() to strictly validate the time parameter against the allowed values before any recommendation logic is executed.
  • Return a 400 Bad Request with a consistent JSON error response (e.g., {"error": "Please select a valid time availability."}) whenever an invalid value is received.
  • Ensure the API contract is preserved so that frontend clients always receive structured JSON responses for invalid input instead of HTML.
  • Add or update tests to verify both valid and invalid time values, ensuring invalid requests return the correct status code and JSON payload while valid requests continue to function as expected.

I'll keep the fix focused, follow the project's existing validation patterns, and submit a PR with the implementation and accompanying tests if assigned.

Ishita-rxi avatar Jun 26 '26 08:06 Ishita-rxi

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions!

github-actions[bot] avatar Nov 21 '26 08:11 github-actions[bot]

This issue was closed because it has been inactive for an extended period. If this issue is still relevant, feel free to reopen it or create a new one.

github-actions[bot] avatar Nov 28 '26 08:11 github-actions[bot]