Fix withDb: re-throw React Router ErrorResponseImpl

React Router's data() throws ErrorResponseImpl, not Response.
Check for objects with status + data properties to catch both
Response and ErrorResponseImpl, preventing withDb from
swallowing 404s as 503s.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ullrich Schäfer 2026-03-25 00:03:15 +01:00
parent b71117e4ca
commit 83009c2b9b
No known key found for this signature in database
GPG key ID: A32FF691A0F752D9

View file

@ -21,7 +21,13 @@ export type Database = ReturnType<typeof createDb>;
export function withDb<T>(handler: () => Promise<T>): Promise<T> {
return handler().catch((error) => {
// Re-throw React Router responses (redirects, data() throws)
if (error instanceof Response) throw error;
// Check for Response, ErrorResponseImpl, or anything with a status property
if (
error instanceof Response ||
(error && typeof error === "object" && "status" in error && "data" in error)
) {
throw error;
}
// Any other error from a DB-wrapped handler is treated as DB unavailable
const message = error instanceof Error ? error.message : String(error);