Skip to content

Fix: Return HTTP 404 for missing users instead of HTTP 500#40

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

Fix: Return HTTP 404 for missing users instead of HTTP 500#40
mw-middleware-labs-sandbox[bot] wants to merge 1 commit into
masterfrom
fix/user-not-found-404-handling

Conversation

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

Problem

The user_profile() endpoint was raising KeyError exceptions when users requested non-existent user profiles, resulting in:

  • HTTP 500 (Internal Server Error) instead of HTTP 404 (Not Found)
  • Error tracking alerts for expected validation scenarios
  • Polluted error logs with non-critical events

Root cause: Line 78 in flask/app.py raised KeyError for missing users

Evidence: Middleware Alert ID 30146661 captured 4 exceptions for requests to user6 and user7

Solution

  • Added abort to Flask imports
  • Check if username exists in user_data before lookup
  • Return HTTP 404 with descriptive message for missing users
  • Removed unreachable error handling code

Impact

✅ Returns semantically correct HTTP 404 for missing users
✅ Prevents error tracking alerts on expected validation failures
✅ Improves API adherence to REST best practices
✅ Reduces noise in error logs

Testing

Test with:

curl -i http://localhost:5000/user/nonexistent
# Should return HTTP 404 instead of HTTP 500

Changes

  • Line 10: Added abort to Flask imports
  • Lines 63-67: Replaced KeyError exception logic with proper 404 handling using abort(404, ...)

OpsAI Context

Open OpsAI Chat: Open Chat

Created By

Name: Bajrang
Email: bajrang.ostwal+sandbox@middleware.io

- Add 'abort' to Flask imports
- Check if username exists in user_data before lookup
- Return proper HTTP 404 with descriptive message for missing users
- Remove unreachable error handling code

This resolves the Python Backend Failure alert (Alert ID: 30146661)
where KeyError exceptions were causing HTTP 500 responses for
non-existent users (user6, user7), triggering error tracking alerts.

Root cause: Improper error handling in user_profile() function
Fix: Use abort(404) instead of raising KeyError for missing users
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