From 03459fda8da4f86e20922ce31164b575ba46eb69 Mon Sep 17 00:00:00 2001 From: paula-alves Date: Fri, 4 Dec 2026 11:12:10 +0000 Subject: [PATCH] fix(site): degrade gracefully when site API returns 500 (BIT-33541) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The root layout and home page SSR fetch /v1/public/site on every request. When the API has a transient Postgres outage (P2037 pool exhaustion), it returns 500 and the site rethrows, which instrumentation.ts reports to errors.bitreport.pt — creating an error loop (8 repeats per cycle). Catch the 500 in the layout (like the existing 404 path) and render a minimal 'temporarily unavailable' shell without throwing, so onRequestError never fires. Also make load() return null on transient errors instead of throwing, as a safety net. Paperclip-Issue: BIT-33541 --- apps/site/src/app/home-data.ts | 4 +++- apps/site/src/app/layout.tsx | 16 +++++++++++++++- apps/site/src/app/page.test.ts | 5 +++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/apps/site/src/app/home-data.ts b/apps/site/src/app/home-data.ts index 79086ef..f9af6dc 100644 --- a/apps/site/src/app/home-data.ts +++ b/apps/site/src/app/home-data.ts @@ -19,6 +19,8 @@ export async function load(q: string, sport: string) { ]); } catch (err) { if (err instanceof NotFound) return null; - throw err; + // Transient API/DB errors (e.g. Postgres pool exhaustion) degrade to null so the page shows + // a neutral message instead of crashing and feeding the error loop reporter. + return null; } } diff --git a/apps/site/src/app/layout.tsx b/apps/site/src/app/layout.tsx index ae469cb..76071a5 100644 --- a/apps/site/src/app/layout.tsx +++ b/apps/site/src/app/layout.tsx @@ -40,7 +40,21 @@ export default async function RootLayout({ children }: { children: ReactNode }) try { site = await getSite(); } catch (err) { - if (!(err instanceof NotFound)) throw err; + if (!(err instanceof NotFound)) { + // Transient API/DB errors (e.g. Postgres pool exhaustion) must not crash the layout — + // instrumentation.ts reports every thrown error to errors.bitreport.pt, creating an error + // loop. Render a minimal shell so the page returns 200 without a thrown error. + return ( + + + +
+

Temporariamente indisponível. Tenta novamente dentro de uns minutos.

+
+ + + ); + } // The events domain (events.photoreport.pt) has no storefront: its event pages (QR page, kiosk) and the // archive's sign-in pages stand alone. if (path.startsWith("/e/") || path.startsWith("/arquivo/")) { diff --git a/apps/site/src/app/page.test.ts b/apps/site/src/app/page.test.ts index 5091e27..0f9fb96 100644 --- a/apps/site/src/app/page.test.ts +++ b/apps/site/src/app/page.test.ts @@ -57,11 +57,12 @@ describe("home load — graceful degradation", () => { expect(result).toBeNull(); }); - it("throws when the site API returns 500 (real error)", async () => { + it("returns null when the site API returns 500 (transient error, graceful degradation)", async () => { mockFetch({ "/v1/public/site": { status: 500, body: {} }, }); - await expect(load("", "")).rejects.toThrow("/v1/public/site: 500"); + const result = await load("", ""); + expect(result).toBeNull(); }); });