mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-25 20:33:11 +00:00
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:
parent
a1cd118be5
commit
a68db7927c
1 changed files with 13 additions and 5 deletions
|
|
@ -9,11 +9,19 @@ import { getCachedGlobal } from '@/utilities/globalReads'
|
||||||
import { absoluteSitePath, resolveSiteMetadata, toAbsoluteUrl, truncate } from '@/utilities/seo'
|
import { absoluteSitePath, resolveSiteMetadata, toAbsoluteUrl, truncate } from '@/utilities/seo'
|
||||||
import type { Metadata } from 'next'
|
import type { Metadata } from 'next'
|
||||||
import { notFound } from 'next/navigation'
|
import { notFound } from 'next/navigation'
|
||||||
import { APIError } from 'payload'
|
|
||||||
|
|
||||||
const MAX_DESCRIPTION_LENGTH = 200
|
const MAX_DESCRIPTION_LENGTH = 200
|
||||||
const CREDIT = 'Fonte: Câmara dos Deputados · CC BY 4.0'
|
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 => {
|
const parseCutId = (raw: string): number | null => {
|
||||||
if (!/^\d+$/.test(raw)) return null
|
if (!/^\d+$/.test(raw)) return null
|
||||||
const id = Number(raw)
|
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
|
if (!cut || cut.status !== 'published' || !mediaOf(cut)?.url) return null
|
||||||
return cut
|
return cut
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// `findByID` throws NotFound for an unknown id; both that and an
|
// Both an unknown id (404) and an unpublished cut resolve to the same
|
||||||
// unpublished cut resolve to the same not-found screen (never reveal
|
// not-found screen (never reveal whether it existed). A real read failure
|
||||||
// whether it existed). A real read failure keeps failing loudly.
|
// keeps failing loudly.
|
||||||
if (error instanceof APIError && error.status === 404) return null
|
if (isNotFoundError(error)) return null
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue