diff --git a/apps/site/src/app/arquivo/[id]/f/[faceId]/page.tsx b/apps/site/src/app/arquivo/[id]/f/[faceId]/page.tsx index e9a73f0..d58c355 100644 --- a/apps/site/src/app/arquivo/[id]/f/[faceId]/page.tsx +++ b/apps/site/src/app/arquivo/[id]/f/[faceId]/page.tsx @@ -10,7 +10,7 @@ const UUID = /^[0-9a-f-]{36}$/i; export async function generateMetadata({ params }: Params): Promise { const { id } = await params; const info = await load(id); - return { title: `Cara · ${info.name}`, robots: { index: false } }; + return { title: info ? `Cara · ${info.name}` : "Arquivo", robots: { index: false } }; } export default async function FacePage({ params }: Params) { diff --git a/apps/site/src/app/arquivo/[id]/frame.tsx b/apps/site/src/app/arquivo/[id]/frame.tsx index d0f9ff7..03fafeb 100644 --- a/apps/site/src/app/arquivo/[id]/frame.tsx +++ b/apps/site/src/app/arquivo/[id]/frame.tsx @@ -5,6 +5,13 @@ import { load } from "./load"; /** The archive shell: the page the URL asked for, in its owner's template (`data-look`, archive.css). */ export async function ArchiveFrame({ id, start }: { id: string; start?: View }) { const info = await load(id); + if (!info) { + return ( +
+

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

+
+ ); + } // On the archive's own address (the middleware rewrote `/g/…` to here) its links stay at the root. const atRoot = (await headers()).get("x-arquivo-root") === "1"; return ; diff --git a/apps/site/src/app/arquivo/[id]/g/[folderId]/page.tsx b/apps/site/src/app/arquivo/[id]/g/[folderId]/page.tsx index d24e1e0..7cdd242 100644 --- a/apps/site/src/app/arquivo/[id]/g/[folderId]/page.tsx +++ b/apps/site/src/app/arquivo/[id]/g/[folderId]/page.tsx @@ -10,7 +10,7 @@ const UUID = /^[0-9a-f-]{36}$/i; export async function generateMetadata({ params }: Params): Promise { const { id } = await params; const info = await load(id); - return { title: `Galeria · ${info.name}`, robots: { index: false } }; + return { title: info ? `Galeria · ${info.name}` : "Arquivo", robots: { index: false } }; } export default async function GalleryPage({ params }: Params) { diff --git a/apps/site/src/app/arquivo/[id]/load.test.ts b/apps/site/src/app/arquivo/[id]/load.test.ts new file mode 100644 index 0000000..879988d --- /dev/null +++ b/apps/site/src/app/arquivo/[id]/load.test.ts @@ -0,0 +1,71 @@ +import { describe, expect, it, vi, afterEach } from "vitest"; + +vi.mock("next/navigation", () => ({ + notFound: () => { + const err = new Error("notFound"); + err.name = "NotFound"; + throw err; + }, +})); + +const { load } = await import("./load"); + +const archiveBody = { + id: "d26ceb8e-c146-4f68-abaf-c79ce3981b2d", + name: "Test Archive", + cloud: "cloud1", + organization: "org1", +}; + +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("archive load — graceful degradation", () => { + const originalFetch = global.fetch; + + afterEach(() => { + global.fetch = originalFetch; + }); + + it("returns archive info on 200", async () => { + mockFetch({ "/v1/archive/": { status: 200, body: archiveBody } }); + const info = await load(archiveBody.id); + expect(info).not.toBeNull(); + expect(info?.name).toBe("Test Archive"); + }); + + it("returns null on 500 (transient error, no throw)", async () => { + mockFetch({ "/v1/archive/": { status: 500, body: {} } }); + const info = await load(archiveBody.id); + expect(info).toBeNull(); + }); + + it("returns null on 502 (transient error, no throw)", async () => { + mockFetch({ "/v1/archive/": { status: 502, body: {} } }); + const info = await load(archiveBody.id); + expect(info).toBeNull(); + }); + + it("returns null on 503 (transient error, no throw)", async () => { + mockFetch({ "/v1/archive/": { status: 503, body: {} } }); + const info = await load(archiveBody.id); + expect(info).toBeNull(); + }); + + it("throws notFound on 404", async () => { + mockFetch({ "/v1/archive/": { status: 404, body: {} } }); + await expect(load(archiveBody.id)).rejects.toThrow("notFound"); + }); + + it("throws notFound on 400", async () => { + mockFetch({ "/v1/archive/": { status: 400, body: {} } }); + await expect(load(archiveBody.id)).rejects.toThrow("notFound"); + }); +}); diff --git a/apps/site/src/app/arquivo/[id]/load.ts b/apps/site/src/app/arquivo/[id]/load.ts index 59f406a..06b6be4 100644 --- a/apps/site/src/app/arquivo/[id]/load.ts +++ b/apps/site/src/app/arquivo/[id]/load.ts @@ -15,10 +15,10 @@ export interface ArchiveInfo { base?: string; } -export async function load(id: string): Promise { +export async function load(id: string): Promise { if (!/^[0-9a-f-]{36}$/i.test(id)) notFound(); const res = await fetch(`${API}/v1/archive/${id}`, { cache: "no-store" }); if (res.status === 404 || res.status === 400) notFound(); - if (!res.ok) throw new Error(`archive ${id}: ${res.status}`); + if (!res.ok) return null; return (await res.json()) as ArchiveInfo; } diff --git a/apps/site/src/app/arquivo/[id]/p/[personId]/page.tsx b/apps/site/src/app/arquivo/[id]/p/[personId]/page.tsx index a7ff1db..65cbf36 100644 --- a/apps/site/src/app/arquivo/[id]/p/[personId]/page.tsx +++ b/apps/site/src/app/arquivo/[id]/p/[personId]/page.tsx @@ -10,7 +10,7 @@ const UUID = /^[0-9a-f-]{36}$/i; export async function generateMetadata({ params }: Params): Promise { const { id } = await params; const info = await load(id); - return { title: `Pessoa · ${info.name}`, robots: { index: false } }; + return { title: info ? `Pessoa · ${info.name}` : "Arquivo", robots: { index: false } }; } export default async function PersonPage({ params }: Params) { diff --git a/apps/site/src/app/arquivo/[id]/page.tsx b/apps/site/src/app/arquivo/[id]/page.tsx index 6b4448b..61d9ab7 100644 --- a/apps/site/src/app/arquivo/[id]/page.tsx +++ b/apps/site/src/app/arquivo/[id]/page.tsx @@ -8,7 +8,7 @@ type Params = { params: Promise<{ id: string }>; searchParams: Promise<{ t?: str export async function generateMetadata({ params }: Params): Promise { const info = await load((await params).id); - return { title: `Arquivo · ${info.name}`, robots: { index: false } }; + return { title: info ? `Arquivo · ${info.name}` : "Arquivo", robots: { index: false } }; } /**