Unverified Commit 45a328a8 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

avoid logging stacks for internal errors (#71575)

`NEXT_PAGE_EXPORT_ERROR` and `NEXT_STATIC_GEN_BAILOUT` shouldn't show up
in failed build logs as they'll point to an internal stack not related
to user code.

This updates the code that throws the `ExportPageError` to instead exit
the worker when `prerenderEarlyExit` is true (which is the default) so
that the page export error doesn't leak into the outer error handler
which is meant for user errors.

For static generation bailout errors, we'll log the error message (if it
exists) otherwise there's nothing to log as the stack would point to
internal framework code.

This also removes a redundant log that is already printed as a prerender
error.
parent 8a905cf0
...@@ -48,6 +48,7 @@ import { ...@@ -48,6 +48,7 @@ import {
import { needsExperimentalReact } from '../lib/needs-experimental-react' import { needsExperimentalReact } from '../lib/needs-experimental-react'
import { runWithCacheScope } from '../server/async-storage/cache-scope.external' import { runWithCacheScope } from '../server/async-storage/cache-scope.external'
import type { AppRouteRouteModule } from '../server/route-modules/app-route/module.compiled' import type { AppRouteRouteModule } from '../server/route-modules/app-route/module.compiled'
import { isStaticGenBailoutError } from '../client/components/static-generation-bailout'
const envConfig = require('../shared/lib/runtime-config.external') const envConfig = require('../shared/lib/runtime-config.external')
...@@ -433,16 +434,17 @@ export async function exportPages( ...@@ -433,16 +434,17 @@ export async function exportPages(
if (attempt >= maxAttempts - 1) { if (attempt >= maxAttempts - 1) {
// Log a message if we've reached the maximum number of attempts. // Log a message if we've reached the maximum number of attempts.
// We only care to do this if maxAttempts was configured. // We only care to do this if maxAttempts was configured.
if (maxAttempts > 0) { if (maxAttempts > 1) {
console.info( console.info(
`Failed to build ${pageKey} after ${maxAttempts} attempts.` `Failed to build ${pageKey} after ${maxAttempts} attempts.`
) )
} }
// If prerenderEarlyExit is enabled, we'll exit the build immediately. // If prerenderEarlyExit is enabled, we'll exit the build immediately.
if (nextConfig.experimental.prerenderEarlyExit) { if (nextConfig.experimental.prerenderEarlyExit) {
throw new ExportPageError( console.error(
`Export encountered an error on ${pageKey}, exiting the build.` `Export encountered an error on ${pageKey}, exiting the build.`
) )
process.exit(1)
} else { } else {
// Otherwise, this is a no-op. The build will continue, and a summary of failed pages will be displayed at the end. // Otherwise, this is a no-op. The build will continue, and a summary of failed pages will be displayed at the end.
} }
...@@ -540,8 +542,21 @@ async function exportPage( ...@@ -540,8 +542,21 @@ async function exportPage(
`\nError occurred prerendering page "${input.path}". Read more: https://nextjs.org/docs/messages/prerender-error\n` `\nError occurred prerendering page "${input.path}". Read more: https://nextjs.org/docs/messages/prerender-error\n`
) )
// bailoutToCSRError errors should not leak to the user as they are not actionable; they're
// a framework signal
if (!isBailoutToCSRError(err)) { if (!isBailoutToCSRError(err)) {
console.error(isError(err) && err.stack ? err.stack : err) // A static generation bailout error is a framework signal to fail static generation but
// and will encode a reason in the error message. If there is a message, we'll print it.
// Otherwise there's nothing to show as we don't want to leak an error internal error stack to the user.
if (isStaticGenBailoutError(err)) {
if (err.message) {
console.error(`Error: ${err.message}`)
}
} else if (isError(err) && err.stack) {
console.error(err.stack)
} else {
console.error(err)
}
} }
return { error: true, duration: Date.now() - start, files: [] } return { error: true, duration: Date.now() - start, files: [] }
......
...@@ -653,9 +653,7 @@ export function throwIfDisallowedDynamic( ...@@ -653,9 +653,7 @@ export function throwIfDisallowedDynamic(
console.error(syncError) console.error(syncError)
} }
// The actual error should have been logged when the sync access ocurred // The actual error should have been logged when the sync access ocurred
throw new StaticGenBailoutError( throw new StaticGenBailoutError()
`Route "${route}" could not be prerendered.`
)
} }
const dynamicErrors = dynamicValidation.dynamicErrors const dynamicErrors = dynamicValidation.dynamicErrors
...@@ -664,9 +662,7 @@ export function throwIfDisallowedDynamic( ...@@ -664,9 +662,7 @@ export function throwIfDisallowedDynamic(
console.error(dynamicErrors[i]) console.error(dynamicErrors[i])
} }
throw new StaticGenBailoutError( throw new StaticGenBailoutError()
`Route "${route}" could not be prerendered.`
)
} }
if (!dynamicValidation.hasSuspendedDynamic) { if (!dynamicValidation.hasSuspendedDynamic) {
......
...@@ -126,7 +126,6 @@ function runTests(options: { withMinification: boolean }) { ...@@ -126,7 +126,6 @@ function runTests(options: { withMinification: boolean }) {
'Error: Route "/" used `Math.random()` outside of `"use cache"` and without explicitly calling `await connection()` beforehand. See more info here: https://nextjs.org/docs/messages/next-prerender-random' 'Error: Route "/" used `Math.random()` outside of `"use cache"` and without explicitly calling `await connection()` beforehand. See more info here: https://nextjs.org/docs/messages/next-prerender-random'
) )
expectError('Error occurred prerendering page "/"') expectError('Error occurred prerendering page "/"')
expectError('Error: Route "/" could not be prerendered.')
expectError('exiting the build.') expectError('exiting the build.')
}) })
}) })
......
...@@ -124,7 +124,6 @@ function runTests(options: { withMinification: boolean }) { ...@@ -124,7 +124,6 @@ function runTests(options: { withMinification: boolean }) {
expectError('Route "/" used `searchParams.foo`') expectError('Route "/" used `searchParams.foo`')
expectError('Error occurred prerendering page "/"') expectError('Error occurred prerendering page "/"')
expectError('Error: Route "/" could not be prerendered.')
expectError('exiting the build.') expectError('exiting the build.')
}) })
}) })
...@@ -212,7 +211,6 @@ function runTests(options: { withMinification: boolean }) { ...@@ -212,7 +211,6 @@ function runTests(options: { withMinification: boolean }) {
expectError('Route "/" used `searchParams.foo`') expectError('Route "/" used `searchParams.foo`')
expectError('Error occurred prerendering page "/"') expectError('Error occurred prerendering page "/"')
expectError('Error: Route "/" could not be prerendered.')
expectError('exiting the build.') expectError('exiting the build.')
}) })
}) })
...@@ -300,7 +298,6 @@ function runTests(options: { withMinification: boolean }) { ...@@ -300,7 +298,6 @@ function runTests(options: { withMinification: boolean }) {
expectError('Route "/" used `cookies().get(\'token\')`') expectError('Route "/" used `cookies().get(\'token\')`')
expectError('Error occurred prerendering page "/"') expectError('Error occurred prerendering page "/"')
expectError('Route "/" could not be prerendered.')
expectError('exiting the build.') expectError('exiting the build.')
}) })
}) })
......
...@@ -337,7 +337,6 @@ function runTests(options: { withMinification: boolean }) { ...@@ -337,7 +337,6 @@ function runTests(options: { withMinification: boolean }) {
) )
} }
expectError('Error occurred prerendering page "/"') expectError('Error occurred prerendering page "/"')
expectError('Error: Route "/" could not be prerendered.')
expectError('exiting the build.') expectError('exiting the build.')
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment