fix(C168): reconhece o 404 do /corte/<id> pelo status, não por instanceof

No build de produção o erro do findByID atravessa a fronteira de módulo e o\ninstanceof APIError falha, virando 500 no id inexistente (pego no e2e do CI).\nO contrato é o status: 404 = não encontrado -> notFound(); o resto falha alto.
This commit is contained in:
Francisco Solla 2026-09-15 12:25:42 -03:00
parent c0b83611ee
commit 2e2813fe8c

View file

@ -9,11 +9,19 @@ import { getCachedGlobal } from '@/utilities/globalReads'
import { absoluteSitePath, resolveSiteMetadata, toAbsoluteUrl, truncate } from '@/utilities/seo'
import type { Metadata } from 'next'
import { notFound } from 'next/navigation'
import { APIError } from 'payload'
const MAX_DESCRIPTION_LENGTH = 200
const CREDIT = 'Fonte: Câmara dos Deputados · CC BY 4.0'
/**
* `findByID` answers 404 for an unknown id; on the production (bundled) server
* that error can cross a module boundary where `instanceof APIError` is false,
* so the status is the contract — an unknown id is "not found" (renders 404),
* anything else keeps failing loudly.
*/
const isNotFoundError = (error: unknown): boolean =>
typeof error === 'object' && error !== null && (error as { status?: unknown }).status === 404
const parseCutId = (raw: string): number | null => {
if (!/^\d+$/.test(raw)) return null
const id = Number(raw)
@ -41,10 +49,10 @@ const loadPublishedCut = async (id: string): Promise<SpeechCut | null> => {
if (!cut || cut.status !== 'published' || !mediaOf(cut)?.url) return null
return cut
} catch (error) {
// `findByID` throws NotFound for an unknown id; both that and an
// unpublished cut resolve to the same not-found screen (never reveal
// whether it existed). A real read failure keeps failing loudly.
if (error instanceof APIError && error.status === 404) return null
// Both an unknown id (404) and an unpublished cut resolve to the same
// not-found screen (never reveal whether it existed). A real read failure
// keeps failing loudly.
if (isNotFoundError(error)) return null
throw error
}
}