Can't catch `notFound()` in Next.js v15.1 ???
Answered
Black Caiman posted this in #help-forum
Black CaimanOP
Previously in v15.0.4, I can catch the
notFound()
error by its error message (see code below). It doesn't work anymore in v15.1. Is there any behavior changes in notFound()
? Or this is a bug?const NEXT_NOT_FOUND_ERR_MSG = 'NEXT_NOT_FOUND'
export function isNextNotFoundError(error: unknown) {
return error instanceof Error && error.message === NEXT_NOT_FOUND_ERR_MSG
}
async function fetchData(endpoint: string) {
const response = await fetch(`${API_URL}${endpoint}`)
const result = await response.json()
if (!response.ok) {
if (response.status === 404) {
notFound()
}
throw new Error(`API error: ${result.message} (${response.status})`)
}
return result
}
const { data } = useSuspenseQuery({
queryKey: ['data', id],
queryFn: async () => {
try {
return await fetchData(id)
} catch (error) {
if (isNextNotFoundError(error)) {
return null
}
throw error
}
},
})
Answered by Black Caiman
As I expected, the message has changed
- v15.1 :
- v15.0.4:
- v15.1 :
${HTTP_ERROR_FALLBACK_ERROR_CODE};404
https://github.com/vercel/next.js/blob/dafcd43fac3ef9d0ffd94f9c94fd61db4449df25/packages/next/src/client/components/not-found.ts#L21- v15.0.4:
NEXT_NOT_FOUND
https://github.com/vercel/next.js/blob/d6a6aa14069a497728ccb11e5b8acd8bf93e76f1/packages/next/src/client/components/not-found.ts#L15 Replies
Black CaimanOP
As I expected, the message has changed
- v15.1 :
- v15.0.4:
- v15.1 :
${HTTP_ERROR_FALLBACK_ERROR_CODE};404
https://github.com/vercel/next.js/blob/dafcd43fac3ef9d0ffd94f9c94fd61db4449df25/packages/next/src/client/components/not-found.ts#L21- v15.0.4:
NEXT_NOT_FOUND
https://github.com/vercel/next.js/blob/d6a6aa14069a497728ccb11e5b8acd8bf93e76f1/packages/next/src/client/components/not-found.ts#L1Answer
Black CaimanOP
Maybe we should provide built-in error helper (e.g.
isNotFound()
) in next/navigation
modulethere is https://nextjs.org/docs/app/api-reference/functions/unstable_rethrow, but that's about the closest thing we currently have
@joulev there is <https://nextjs.org/docs/app/api-reference/functions/unstable_rethrow>, but that's about the closest thing we currently have
Black CaimanOP
Do you mean like this?
But does
export function isNextNotFoundError(error: unknown) {
try {
unstable_rethrow(error)
return false
} catch {
return true
}
}
But does
unstable_rethrow()
only take errors from notFound()
? What about the upcoming forbidden()
and unauthorized()
?@Black Caiman Do you mean like this?
ts
export function isNextNotFoundError(error: unknown) {
try {
unstable_rethrow(error)
return false
} catch {
return true
}
}
But does `unstable_rethrow()` only take errors from `notFound()`? What about the upcoming `forbidden()` and `unauthorized()`?
you can do it without the the wrapper.
regarding forbidden() and unauthorized(), if it throws an error to perform a redirect, then yees it should capture it too
unstable_rethrow
is meant to take parameter from catch(error)
.regarding forbidden() and unauthorized(), if it throws an error to perform a redirect, then yees it should capture it too