Skip to content

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

@Vinayak051

Description

@Vinayak051

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

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions