Don't cache error responses, and return accurate error codes from Arctic hydrology endpoints - #738
Merged
Merged
Conversation
…tic hydrology endpoints Error responses were cached with the same 90-day max-age as successful responses, so a transient upstream failure could be served from cache long after the upstream recovered. Errors now get a 60-second max-age instead; 200s keep the 90-day TTL. Also, get_features() in the Arctic hydrology routes returned 400 Bad Request for every failure. It now distinguishes a WFS/upstream fetch failure (502) from a stream ID with no features (404). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
cstephen
approved these changes
Aug 3, 2026
cstephen
left a comment
Contributor
There was a problem hiding this comment.
This all looks good to me. The code diff makes sense, and I've confirmed that the Cache-Control: max-age=is set to to the appropriate length of time for HTTP 200 vs. 404 or 502 status codes. (The Data API won't start with a bad GeoServer URL domain, so I had to test the HTTP 502 status codes by running against the production GeoServer URL, but temporarily changed the generate_wfs_arctic_hydrology_url function to return bogus URLs.)
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.
Note
This PR was AI-generated (Claude Code) and human-reviewed before submission.
Summary
Two related fixes around error handling:
after_requesthook setCache-Control: max-age=7776000on every response, including errors. A transient upstream hiccup could therefore be served from cache for months after the upstream recovered. Now only 200 responses get the 90-day max-age; everything else gets a 60-second max-age — short enough to recover quickly, long enough to shield upstreams from request storms during a real outage.get_features()inroutes/arctic_hydrology.pyreturned400 Bad Requestfor every failure, whether the WFS fetch to GeoServer failed or the stream ID simply had no features. It now returns 502 Upstream Data Unreachable for fetch failures and 404 No Data for unknown stream IDs (both using existing templates). The twoisinstance(gdf, tuple)early-return comments in the stats and modeled-climatology routes are updated to match.Manual testing
curl -sI http://localhost:5000/arctic_hydrology/stats/81000004— expect200andCache-Control: max-age=7776000.curl -sI http://localhost:5000/arctic_hydrology/stats/1(a stream ID with no features) — expect404andCache-Control: max-age=60, and the "No data" page in a browser.export API_GS_BASE_URL=http://localhost:9999/, then repeat the request from step 2 — expect502andCache-Control: max-age=60, and the "Upstream Data Unreachable" page in a browser. UnsetAPI_GS_BASE_URLafterwards.Cache-Control: max-age=7776000on success.🤖 Generated with Claude Code