Skip to content

Fix: Handle missing users gracefully in /user/<username> endpoint#39

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

Fix: Handle missing users gracefully in /user/<username> endpoint#39
mw-middleware-labs-sandbox[bot] wants to merge 1 commit into
masterfrom
fix/user-profile-keyerror

Conversation

@mw-middleware-labs-sandbox
Copy link
Copy Markdown

Problem

The user_profile function raises a KeyError when a non-existent user is requested, resulting in a 500 Internal Server Error instead of a proper 404 Not Found response.

Root Cause

Direct dictionary access user_data[username] at line 63 throws KeyError when the username doesn't exist in the dictionary.

Solution

  • Replace direct dictionary access with .get() method for safe lookup
  • Return 404 Not Found with descriptive error message when user is not found
  • Maintain existing behavior for valid users

Impact

  • Prevents unhandled KeyError exceptions
  • Returns proper HTTP 404 status for missing users instead of 500
  • Improves API semantics and user experience

Testing

  • Valid user request: GET /user/user1 → 200 OK with profile data
  • Missing user request: GET /user/user7 → 404 Not Found with error message

Fixes issue identified in APM trace: KeyError: "User 'user7' not found"


OpsAI Context

Open OpsAI Chat: Open Chat

Created By

Name: Sanjay
Email: sanjay+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