fix(console): distinguish DB-down from rate-limiter-unavailable in 503 error#1420
Draft
vklimontovich wants to merge 2 commits into
Draft
fix(console): distinguish DB-down from rate-limiter-unavailable in 503 error#1420vklimontovich wants to merge 2 commits into
vklimontovich wants to merge 2 commits into
Conversation
Replace the hardcoded P1xxx code list with an active `SELECT 1` probe (`isDatabaseReachable`). Prisma exposes no "am I connected?" flag, so probing is the only reliable signal and there is no error-code list to keep in sync. Move DB-down handling into the shared top-level catch so every route, not just rate-limited ones, returns `database_unavailable` when Postgres is down. The rate-limiter catch reuses the same probe. Fail-closed behavior is unchanged.
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
When the database is down/unreachable, API requests returned a misleading 503:
This points developers at the wrong subsystem. And it only surfaced on
rate-limited routes — a DB outage on any other route returned a generic 500.
Root cause
The console rate limiter is Postgres-backed (
PgRateLimiter) — it runs anINSERT ... ON CONFLICTon every rate-limited request. The limiter check inlib/api.tsis intentionally fail-closed: ifcheck()throws for any reason,the request is rejected with 503. When Postgres is down, that catch swallowed
the real cause behind the generic "rate limiter unavailable" message.
Fix
Detect a DB outage by actively probing the database, and report it clearly
and consistently across every route.
lib/server/db.ts: newisDatabaseReachable(timeoutMs = 1500)— runs atrivial
SELECT 1bounded by a timeout. Prisma exposes no "am I connected?"flag (connections are lazy + pooled), so a probe is the only reliable signal,
and there is no Prisma error-code list to keep in sync across versions.
lib/api.ts: DB-down handling is centralized in the shared top-level catch(
respondIfDatabaseDown), so every route — not just rate-limited ones —returns
error: "database_unavailable"/ "Database is unavailable; requestrejected." when Postgres is down. The rate-limiter catch reuses the same probe.
Retry-After: 5).Note
The probe runs one
SELECT 1on the error path (unexpected 500s and limiterfailures). Cheap and exception-path-only; if a real outage's probe stampede ever
matters, a short negative-result cache on
isDatabaseReachablewould collapse it.