fix(routes): return 500 on DB failure instead of 404 during route lookup#1036
fix(routes): return 500 on DB failure instead of 404 during route lookup#10363rabiii wants to merge 2 commits into
Conversation
📝 WalkthroughWalkthroughThe handler now distinguishes sql.ErrNoRows (returns 404) from other DB errors (returns 500) and separately checks for empty route.ID; a new test forces a DB failure and asserts an HTTP 500 response. ChangesRoute Lookup Error Handling
Sequence DiagramsequenceDiagram
participant Client
participant routeHandler
participant GetRoute
participant Database
Client->>routeHandler: GET /route
routeHandler->>GetRoute: fetch route
GetRoute->>Database: query
alt sql.ErrNoRows
Database-->>GetRoute: sql.ErrNoRows
GetRoute-->>routeHandler: error
routeHandler-->>Client: 404 Not Found
else other DB error
Database-->>GetRoute: other error
GetRoute-->>routeHandler: error
routeHandler-->>Client: 500 Internal Server Error
else empty route.ID
GetRoute-->>routeHandler: route.ID == ""
routeHandler-->>Client: 404 Not Found
else success
Database-->>GetRoute: route found
GetRoute-->>routeHandler: route object
routeHandler-->>Client: 200 OK with route data
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
internal/restapi/route_handler_test.go (1)
116-146: ⚡ Quick winConsider adding test coverage for GetAgency database errors.
TestRouteHandler_DatabaseErrorcorrectly verifies that database failures duringGetRoutereturn HTTP 500. However, the handler now also callsGetAgencywhenincludeReferences=true(the default), and that call has the same error handling pattern.For completeness, consider adding a similar test that verifies the
GetAgencyerror path also returns 500 on database failures (e.g., close the DB after the route is loaded but before the agency lookup, or use a route with a valid ID but request includeReferences=true with a closed DB).🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@internal/restapi/route_handler_test.go` around lines 116 - 146, Add a new unit test similar to TestRouteHandler_DatabaseError that specifically exercises the GetAgency database error path when includeReferences=true: create the same setup (internalgtfs.InitGTFSManager, NewRestAPI), ensure a valid route ID is used, then close manager.GtfsDB after the route is available but before the agency lookup (or call the handler with includeReferences=true and a closed DB) and use callAPIHandler[RouteEntryResponse] (and routeURL) to invoke the endpoint; assert resp.StatusCode and model.Code are http.StatusInternalServerError to verify GetAgency errors propagate a 500. Ensure the test name and assertions mirror TestRouteHandler_DatabaseError (e.g., TestRouteHandler_AgencyDatabaseError) so it covers the agency error path invoked by GetAgency when includeReferences=true.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@internal/restapi/route_handler_test.go`:
- Around line 116-146: Add a new unit test similar to
TestRouteHandler_DatabaseError that specifically exercises the GetAgency
database error path when includeReferences=true: create the same setup
(internalgtfs.InitGTFSManager, NewRestAPI), ensure a valid route ID is used,
then close manager.GtfsDB after the route is available but before the agency
lookup (or call the handler with includeReferences=true and a closed DB) and use
callAPIHandler[RouteEntryResponse] (and routeURL) to invoke the endpoint; assert
resp.StatusCode and model.Code are http.StatusInternalServerError to verify
GetAgency errors propagate a 500. Ensure the test name and assertions mirror
TestRouteHandler_DatabaseError (e.g., TestRouteHandler_AgencyDatabaseError) so
it covers the agency error path invoked by GetAgency when
includeReferences=true.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 4e365ad6-f594-49e1-87fb-43c4b86b8ac6
📒 Files selected for processing (2)
internal/restapi/route_handler.gointernal/restapi/route_handler_test.go



Description
Previously, the
routeHandlertreated all errors returned byGetRouteas a404 Not Found. This was actively swallowing underlying database or connection errors and disguising them as missing records, making it difficult to debug actual system failures in production.This PR updates the handler to properly distinguish between a missing record and a genuine internal server error.
Changes Made:
route_handler.go): * Refactored the error checking logic usingerrors.Is(err, sql.ErrNoRows).sql.ErrNoRowscontinues to correctly return a404 Not Found.500 Internal Server Error.route.ID == ""fallback check as an additional safety net.Tests:
TestRouteHandler_DatabaseError: Created a new integration test to verify the 500 response behavior.fixes: #1009
Summary by CodeRabbit
Bug Fixes
Tests