fix(api): answer 503 when the database node is paused, not a blanket 500 - #45
Merged
Conversation
SQLite Cloud parks a free-tier node after a stretch of inactivity. Every
query then fails with error 10010, so `/api/coupons`, `/api/stores` and
`/api/search` all returned `{"error":"Failed to fetch …"}` with a 500 —
indistinguishable from a real defect, and the CLI rendered it as
`curl: (22) The requested URL returned error: 500`. Nothing in that chain
says "the database is down, wait a minute", so it reads as c0upons being
broken.
isDbPaused() recognises the state (error code first, message as a
fallback) and dbErrorResponse() maps it to 503 + Retry-After: 60 with a
stable `database_paused` code, leaving every other failure on its
existing 500 and message. It is deliberately kept out of isDisconnect():
a paused node refuses new sockets too, so the reconnect-and-retry path
would just pay the connection cost twice before failing identically.
The CLI grows an api_get() mirroring api_post(): read commands now print
the server's own sentence, with distinct wording for 503 so people retry
instead of reinstalling. -L preserves the redirect-following the old
`curl -fsSL` had.
Also fixes a pre-existing exit-code bug: `[ "$i" -ge N ] && break` was the
last command in the search/latest loops, so its status 1 on the final pass
became the script's exit code and a perfectly successful `c0upons latest`
reported failure to `&&` and CI.
Verified against the live paused node: 503 + database_paused on all three
read endpoints, CLI prints the readable message, and the success and 500
paths still exit 0 and 1.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
ThreatCrush Security Scan17 finding(s) MEDIUM: 17
Snippets are redacted; ThreatCrush never prints matched credential material. |
ralyodio
added a commit
that referenced
this pull request
Aug 11, 2026
Submitting a coupon fails with "The coupon database is temporarily unavailable" because the SQLite Cloud node is parked: every query, and every new connection, answers error 10010 until someone restarts it from the dashboard. The 503 mapping added in #45 reports that state correctly — but nothing stops it happening, and it cannot be recovered in-process because a parked node refuses new sockets too. Prevention is the automatable half. /api/health/db runs a SELECT 1, and a scheduled workflow calls it every 15 minutes, so the node never accumulates enough idle time to be parked. The same endpoint doubles as monitoring: an outage now fails a workflow run loudly instead of silently emptying pages that swallow their own DB errors. The schedule is best-effort by nature — GitHub delays scheduled runs under load and disables them after 60 days of repo inactivity — so it reduces the odds of a pause rather than eliminating them. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
What was actually wrong
c0upons latestreportedcurl: (22) The requested URL returned error: 500. The API was not broken — the SQLite Cloud free-tier node is paused (errorCode: 10010), which I confirmed three ways: connecting directly with the prodSQLITECLOUD_URL, the SQLite Cloud HTTP API, and the Railway logs.Restarting the node is still a manual dashboard action — this PR does not fix the outage. It fixes the fact that the outage was indistinguishable from a bug at every layer.
Changes
isDbPaused()(lib/db.ts) recognises the state — error code first, message as fallback. Deliberately not folded intoisDisconnect(): a paused node refuses new sockets too, so the reconnect-and-retry path would pay the connection cost twice before failing identically.dbErrorResponse()(lib/api-error.ts) maps it to503+Retry-After: 60and a stabledatabase_pausedcode. Every other failure keeps its existing 500 and message, so a genuine defect still reads as a defect. Applied to all 11 catch sites across the 8 non-webhook API routes.CLI gains
api_get(), mirroring the existingapi_post(). Read commands now print the server's own sentence instead of a raw curl status, with distinct wording for 503 so people retry rather than reinstall.-Lpreserves the redirect-following the oldcurl -fsSLhad.curl: (22) … error: 500c0upons is temporarily unavailable. The coupon database is temporarily unavailable and should be back shortly.curl: (22) … error: 500Error (500): Failed to fetch couponsDrive-by: exit codes were inverted on success
Pre-existing, unrelated to the outage.
[ "$i" -ge N ] && breakwas the last command in thesearch/latestloops, so on the final pass its status1became the script's exit code — a fully successfulc0upons latestexited1, meaningc0upons latest && echo oknever fired and CI would read success as failure. Verified on stock v1.1.0 before changing it.Verification
Ran the app against the live paused node:
/api/coupons,/api/stores,/api/searchall 503. CLI prints the readable message. Against mock backends: success paths exit0, 500 exits1, 503 exits1.tsc --noEmitclean;eslintshows only the 12 pre-existing errors inpage.tsx/offline/privacy/terms— none in touched files.CLI bumped to v1.2.0 (
public/cli/c0uponsis whatinstall.shandc0upons upgradeserve).🤖 Generated with Claude Code