Fix withDb: catch all non-Response errors as DB failures
The previous version checked for specific error messages that
didn't match Drizzle's actual error text ("Failed query").
Now any error from a withDb-wrapped handler returns 503.
React Router Response objects are still re-thrown.
Verified: DB down → 503 JSON, DB up → 201, home page always 200.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
8fb7771223
commit
2d0a4ac2c7
1 changed files with 11 additions and 15 deletions
|
|
@ -20,21 +20,17 @@ export type Database = ReturnType<typeof createDb>;
|
|||
*/
|
||||
export function withDb<T>(handler: () => Promise<T>): Promise<T> {
|
||||
return handler().catch((error) => {
|
||||
if (error instanceof Response) throw error; // Re-throw React Router responses (redirects, data())
|
||||
const message = error instanceof Error ? error.message : "Unknown error";
|
||||
if (
|
||||
message.includes("connect") ||
|
||||
message.includes("ECONNREFUSED") ||
|
||||
message.includes("DrizzleQueryError") ||
|
||||
message.includes("AggregateError") ||
|
||||
message.includes("database")
|
||||
) {
|
||||
throw new Response(JSON.stringify({ error: "Database unavailable" }), {
|
||||
status: 503,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
throw error;
|
||||
// Re-throw React Router responses (redirects, data() throws)
|
||||
if (error instanceof Response) throw error;
|
||||
|
||||
// Any other error from a DB-wrapped handler is treated as DB unavailable
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
console.error("[withDb] Database error:", message);
|
||||
|
||||
throw new Response(JSON.stringify({ error: "Database unavailable" }), {
|
||||
status: 503,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue