Skip to content

[Bug] UnboundLocalError When modelname or casename is None in DataFileRoute Endpoints #252

@tejassinghbhati

Description

@tejassinghbhati

Summary

Several API endpoints in DataFileRoute.py (including /cleanUp, /resultsExists, /createCaseRun, /updateViews, and others) check conditions like:

if modelname != None or casename != None:

but fail to handle the case where the value is None.

When modelname or casename is None (for example, if the frontend sends a malformed request or the session is empty), the code skips the if block where the response variable is defined and drops directly into:

return jsonify(response), 200

Because response was never assigned a value, the server crashes with a fatal UnboundLocalError.

Expected behavior

If a required parameter like casename or modelname is None or omitted from the JSON payload, the API should handle it gracefully and return a structured error such as:

HTTP 400 Bad Request
{
  "message": "Missing required parameter: modelname",
  "status_code": "error"
}

This prevents a server crash and provides meaningful feedback to the client.

Reproduction steps

  1. Start the Flask application:
python app.py
  1. Open a new PowerShell terminal and send a POST request with null data:
Invoke-RestMethod `
  -Uri "http://127.0.0.1:5002/cleanUp" `
  -Method POST `
  -Headers @{"Content-Type"="application/json"} `
  -Body '{"modelname": null}'
  1. Observe the response:
HTTP 500 Internal Server Error
  1. Check the terminal where python app.py is running.

You will see an unhandled traceback ending with:

UnboundLocalError: local variable 'response' referenced before assignment

(or on Python 3.11+)

UnboundLocalError: cannot access local variable 'response' where it is not associated with a value

Root Cause

The route logic only defines response inside a conditional block but still attempts to return it even when the condition fails.

Example pattern found in multiple endpoints:

if modelname != None:
    response = {...}

return jsonify(response), 200

When modelname is None, response is never created.


Root Cause

The route logic only defines response inside a conditional block but still attempts to return it even when the condition fails.

Example pattern found in multiple endpoints:

if modelname != None:
    response = {...}

return jsonify(response), 200

When modelname is None, response is never created.

Suggested Fix

Validate input parameters at the beginning of each endpoint and return a 400 Bad Request if they are missing or None.

Example:

modelname = request.json.get("modelname")

if modelname is None:
    return jsonify({
        "message": "Missing required parameter: modelname",
        "status_code": "error"
    }), 400

Alternatively, initialize response before conditional logic or restructure the control flow to ensure it is always defined.

Environment

Environment

Field Value
OS Windows / Linux / macOS
Python Python 3.11+
Branch main (latest commit)
Affected File API/Routes/DataFile/DataFileRoute.py

Impact

  • Causes 500 server crashes from malformed or incomplete client requests
  • Affects multiple endpoints in DataFileRoute.py
  • Prevents proper API error handling and client-side debugging

Logs or screenshots

Image Image Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions