From 932b9db2335bf1133f6f0e0a51952fd7a392025b Mon Sep 17 00:00:00 2001 From: paula-alves Date: Fri, 4 Dec 2026 09:59:56 +0000 Subject: [PATCH] Degrade gracefully when galleries API returns 5xx (BIT-33535) The site home page SSR fetches galleries, latest, and popular in Promise.all. When the API has a transient Postgres outage, the galleries endpoint returns 500 and the site rethrows it, tripping the Error Loop reporter. Wrap non-critical fetches (galleries, latest, popular) with a safe() helper that returns empty data on server errors while keeping 404 -> NotFound and 500 from getSite() as real errors. Paperclip-Issue: BIT-33535 --- apps/site/src/app/home-data.ts | 24 ++++++++++++ apps/site/src/app/page.test.ts | 67 ++++++++++++++++++++++++++++++++++ apps/site/src/app/page.tsx | 11 +----- 3 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 apps/site/src/app/home-data.ts create mode 100644 apps/site/src/app/page.test.ts diff --git a/apps/site/src/app/home-data.ts b/apps/site/src/app/home-data.ts new file mode 100644 index 0000000..79086ef --- /dev/null +++ b/apps/site/src/app/home-data.ts @@ -0,0 +1,24 @@ +import type { LatestPhoto, PopularGallery, PublicGallerySummary } from "@photoreport/contracts"; +import { getGalleries, getLatest, getPopular, getSite, NotFound } from "../lib/api"; + +function safe(p: Promise, fallback: T): Promise { + return p.catch((err) => { + if (err instanceof NotFound) throw err; + return fallback; + }); +} + +export async function load(q: string, sport: string) { + const filtered = !!(q || sport); + try { + return await Promise.all([ + getSite(), + safe(getGalleries({ q, sport }), { galleries: [] as PublicGallerySummary[], sports: [] as string[] }), + filtered ? Promise.resolve([] as LatestPhoto[]) : safe(getLatest(), [] as LatestPhoto[]), + filtered ? Promise.resolve([] as PopularGallery[]) : safe(getPopular(), [] as PopularGallery[]), + ]); + } catch (err) { + if (err instanceof NotFound) return null; + throw err; + } +} diff --git a/apps/site/src/app/page.test.ts b/apps/site/src/app/page.test.ts new file mode 100644 index 0000000..5091e27 --- /dev/null +++ b/apps/site/src/app/page.test.ts @@ -0,0 +1,67 @@ +import { describe, expect, it, vi, afterEach, beforeEach } from "vitest"; + +vi.mock("next/headers", () => ({ + headers: () => Promise.resolve(new Headers({ host: "test.local" })), + cookies: () => Promise.resolve({ get: () => undefined }), +})); + +const { load } = await import("./home-data"); + +const siteBody = { + id: "s1", + domain: "test.local", + skin: "photoreport", + name: "Test", + settings: {}, +}; + +function mockFetch(responses: Record) { + global.fetch = vi.fn(async (url: string | URL) => { + const path = new URL(url).pathname; + const match = Object.entries(responses).find(([key]) => path.includes(key)); + if (!match) return new Response("{}", { status: 404 }); + const { status, body } = match[1]; + return new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } }); + }) as typeof fetch; +} + +describe("home load — graceful degradation", () => { + const originalFetch = global.fetch; + + afterEach(() => { + global.fetch = originalFetch; + }); + + it("returns empty galleries when the galleries API returns 500", async () => { + mockFetch({ + "/v1/public/site": { status: 200, body: siteBody }, + "/v1/public/galleries": { status: 500, body: {} }, + "/v1/public/latest": { status: 200, body: { photos: [] } }, + "/v1/public/popular": { status: 200, body: { galleries: [] } }, + }); + + const result = await load("", ""); + expect(result).not.toBeNull(); + if (!result) return; + const [, galleries] = result; + expect(galleries.galleries).toEqual([]); + expect(galleries.sports).toEqual([]); + }); + + it("returns null when the site API returns 404 (unconfigured shop)", async () => { + mockFetch({ + "/v1/public/site": { status: 404, body: {} }, + }); + + const result = await load("", ""); + expect(result).toBeNull(); + }); + + it("throws when the site API returns 500 (real error)", async () => { + mockFetch({ + "/v1/public/site": { status: 500, body: {} }, + }); + + await expect(load("", "")).rejects.toThrow("/v1/public/site: 500"); + }); +}); diff --git a/apps/site/src/app/page.tsx b/apps/site/src/app/page.tsx index 0ca98e6..038a178 100644 --- a/apps/site/src/app/page.tsx +++ b/apps/site/src/app/page.tsx @@ -1,22 +1,13 @@ import Link from "next/link"; import { getGalleries, getLatest, getPopular, getSite, NotFound } from "../lib/api"; import { eventTitle, longDate } from "../lib/format"; +import { load } from "./home-data"; export const dynamic = "force-dynamic"; const SPORT_NAMES: Record = { andebol: "Andebol", futebol: "Futebol", futsal: "Futsal", basquetebol: "Basquetebol", voleibol: "Voleibol", hoquei: "Hóquei", "hóquei em patins": "Hóquei em patins", rugby: "Râguebi", atletismo: "Atletismo" }; const sportName = (s: string) => SPORT_NAMES[s] ?? s.charAt(0).toUpperCase() + s.slice(1); -async function load(q: string, sport: string) { - try { - const filtered = !!(q || sport); - return await Promise.all([getSite(), getGalleries({ q, sport }), filtered ? Promise.resolve([]) : getLatest(), filtered ? Promise.resolve([]) : getPopular()]); - } catch (err) { - if (err instanceof NotFound) return null; - throw err; - } -} - /** Home: search, sport shortcuts, the photos that just arrived, then the galleries. */ export default async function Home({ searchParams }: { searchParams: Promise<{ q?: string; desporto?: string }> }) { const params = await searchParams;