Skip to content

[OpsAI] Fix unhandled KeyError in /user/<username> route -> return 404 - #42

Open
mw-middleware-labs-sandbox[bot] wants to merge 1 commit into
masterfrom
OpsAI/fix-user-profile-404
Open

[OpsAI] Fix unhandled KeyError in /user/<username> route -> return 404#42
mw-middleware-labs-sandbox[bot] wants to merge 1 commit into
masterfrom
OpsAI/fix-user-profile-404

Conversation

@mw-middleware-labs-sandbox

Copy link
Copy Markdown

Root Cause

The GET /user/<username> route in flask/app.py (user_profile function) accessed the in-memory user_data dict via unguarded user_data[username]. When the requested username did not exist (e.g. user7), this raised an unhandled KeyError, resulting in an HTTP 500 error instead of a proper client-facing 404.

This was confirmed via trace data showing deterministic 500 responses for /user/user7 requests, with the stack trace pointing to app.py line 78 in user_profile.

Fix

  • Replaced user_data[username] with user_data.get(username).
  • Added a check: if the lookup returns None, call abort(404, description=f"User '{username}' not found").
  • Added abort to the Flask imports.

Test Cases

  • GET /user/user1 -> 200 (unchanged behavior for existing users)
  • GET /user/user7 -> 404 with descriptive message (previously 500 with unhandled KeyError)

OpsAI Context

Open OpsAI Chat: Open Chat

Created By

Name: Sandbox
Email: sandbox@middleware.io

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.

0 participants