fix: correct error handling in read endpoints to return proper HTTP s…#163
Open
bhudevbhanpuriya wants to merge 1 commit into
Open
fix: correct error handling in read endpoints to return proper HTTP s…#163bhudevbhanpuriya wants to merge 1 commit into
bhudevbhanpuriya wants to merge 1 commit into
Conversation
…tatus codes - Replace broad 'except Exception' with specific exception handlers - Return 404 only for StopAsyncIteration (no results found) - Return 503 for database connection errors - Return 500 for unexpected internal errors - Add logging for all exceptions to improve observability - Apply fix consistently across all 10 read endpoints Fixes issue where all runtime failures were incorrectly mapped to 404, making it impossible to distinguish between legitimate 'not found' and backend/service errors.
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.
Summary
This PR fixes a critical bug in all read endpoints where a broad exception handler was incorrectly mapping all runtime failures to HTTP 404 Not Found, including database connection errors and internal server errors. This made it impossible for clients to distinguish between legitimate "not found" scenarios and actual backend failures.
Problem
All read endpoints in
api/app/v1/endpoints/read/had the following problematic pattern:This caused several issues:
Solution
Implemented specific exception handling to return appropriate HTTP status codes:
Added proper logging for all exceptions to improve observability.
Changes
Modified Files (10)
api/app/v1/endpoints/read/sensor.pyapi/app/v1/endpoints/read/thing.pyapi/app/v1/endpoints/read/datastream.pyapi/app/v1/endpoints/read/observation.pyapi/app/v1/endpoints/read/historical_location.pyapi/app/v1/endpoints/read/feature_of_interest.pyapi/app/v1/endpoints/read/location.pyapi/app/v1/endpoints/read/observed_property.pyapi/app/v1/endpoints/read/network.pyapi/app/v1/endpoints/read/commit.pyKey Changes Per File
import loggingandimport asyncpglogger = logging.getLogger(__name__)except Exceptionwith specific exception handlersStopAsyncIterationhandler for legitimate 404 casesImpact
Before
After
Benefits
Breaking Changes
None. This only affects error responses, and the change makes them more correct according to HTTP standards.
Closes #161