Fix withDb: detect DataWithResponseInit from data() throws

React Router's data() throw creates a DataWithResponseInit object
(type + data + init), not a Response or ErrorResponseImpl.
Check for type === "DataWithResponseInit" to re-throw correctly.

Verified: nonexistent session → 404, DB down → 503.

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

View file

@ -20,12 +20,12 @@ export type Database = ReturnType<typeof createDb>;
*/
export function withDb<T>(handler: () => Promise<T>): Promise<T> {
return handler().catch((error) => {
// Re-throw anything that looks like a React Router response:
// - Response (redirects)
// - ErrorResponseImpl from data() throws (has status + data)
// Re-throw React Router responses and data() throws:
// - Response instances (redirects, manual responses)
// - DataWithResponseInit from data() throws (type + data + init)
if (
error instanceof Response ||
(error != null && typeof error === "object" && "status" in error)
(error != null && typeof error === "object" && error.type === "DataWithResponseInit")
) {
throw error;
}
@ -34,7 +34,6 @@ export function withDb<T>(handler: () => Promise<T>): Promise<T> {
const message = error instanceof Error ? error.message : String(error);
console.error("[withDb] Database error:", message);
// Use the same shape as data() throw so isRouteErrorResponse works
throw new Response("Database unavailable", { status: 503, statusText: "Service Unavailable" });
});
}