Merge pull request 'fix(site): degrade gracefully when archive API returns 5xx (BIT-33540)' (#326) from fix/BIT-33540-arquivo-500-graceful-degradation into main
This commit was merged in pull request #326.
This commit is contained in:
commit
aebf27f155
7 files changed
+84
-6
No files matched your search
@@ -10,7 +10,7 @@ const UUID = /^[0-9a-f-]{36}$/i;
|
||||
export async function generateMetadata({ params }: Params): Promise<Metadata> {
|
||||
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) {
|
||||
|
||||
@@ -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 (
|
||||
<main className="wrap empty">
|
||||
<p>Temporariamente indisponível. Tenta novamente dentro de uns minutos.</p>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
// 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 <ArchiveView info={{ ...info, base: atRoot ? "" : `/arquivo/${info.id}` }} start={start} />;
|
||||
|
||||
@@ -10,7 +10,7 @@ const UUID = /^[0-9a-f-]{36}$/i;
|
||||
export async function generateMetadata({ params }: Params): Promise<Metadata> {
|
||||
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) {
|
||||
|
||||
@@ -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<string, { status: number; body: unknown }>) {
|
||||
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");
|
||||
});
|
||||
});
|
||||
@@ -15,10 +15,10 @@ export interface ArchiveInfo {
|
||||
base?: string;
|
||||
}
|
||||
|
||||
export async function load(id: string): Promise<ArchiveInfo> {
|
||||
export async function load(id: string): Promise<ArchiveInfo | null> {
|
||||
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;
|
||||
}
|
||||
@@ -10,7 +10,7 @@ const UUID = /^[0-9a-f-]{36}$/i;
|
||||
export async function generateMetadata({ params }: Params): Promise<Metadata> {
|
||||
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) {
|
||||
|
||||
@@ -8,7 +8,7 @@ type Params = { params: Promise<{ id: string }>; searchParams: Promise<{ t?: str
|
||||
|
||||
export async function generateMetadata({ params }: Params): Promise<Metadata> {
|
||||
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 } };
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user