Fix: Return HTTP 404 for missing users instead of HTTP 500#40
Open
mw-middleware-labs-sandbox[bot] wants to merge 1 commit into
Open
Fix: Return HTTP 404 for missing users instead of HTTP 500#40mw-middleware-labs-sandbox[bot] wants to merge 1 commit into
mw-middleware-labs-sandbox[bot] wants to merge 1 commit into
Conversation
- 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
user_profile()endpoint was raisingKeyErrorexceptions when users requested non-existent user profiles, resulting in:Root cause: Line 78 in
flask/app.pyraisedKeyErrorfor missing usersEvidence: Middleware Alert ID 30146661 captured 4 exceptions for requests to
user6anduser787d91b0053a48f58314fe5da15969183,ccd931d37c1f50d48b9dd318dfaad3ad,6af879c070957ded75792488460679fa,d4e957f8384ae2b3ebf82a187e0cb110Solution
abortto Flask importsuser_databefore lookupImpact
✅ 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 500Changes
abortto Flask importsabort(404, ...)OpsAI Context
Open OpsAI Chat: Open Chat
Created By
Name: Bajrang
Email:
bajrang.ostwal+sandbox@middleware.io