Unverified Commit b8460266 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Handle getStaticPaths error inside worker to avoid serializing (#39032)

* Handle getStaticPaths error inside worker to avoid serializing

* handle invalid export case
parent cee2cf37
...@@ -862,7 +862,8 @@ export async function isPageStatic( ...@@ -862,7 +862,8 @@ export async function isPageStatic(
traceExcludes?: string[] traceExcludes?: string[]
}> { }> {
const isPageStaticSpan = trace('is-page-static-utils', parentId) const isPageStaticSpan = trace('is-page-static-utils', parentId)
return isPageStaticSpan.traceAsyncFn(async () => { return isPageStaticSpan
.traceAsyncFn(async () => {
require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig) require('../shared/lib/runtime-config').setConfig(runtimeEnvConfig)
setHttpAgentOptions(httpAgentOptions) setHttpAgentOptions(httpAgentOptions)
...@@ -973,6 +974,13 @@ export async function isPageStatic( ...@@ -973,6 +974,13 @@ export async function isPageStatic(
traceExcludes: config.unstable_excludeFiles || [], traceExcludes: config.unstable_excludeFiles || [],
} }
}) })
.catch((err) => {
if (err.message === 'INVALID_DEFAULT_EXPORT') {
throw err
}
console.error(err)
throw new Error(`Failed to collect page data for ${page}`)
})
} }
export async function hasCustomGetInitialProps( export async function hasCustomGetInitialProps(
......
/* eslint-env jest */ /* eslint-env jest */
import fs from 'fs-extra' import fs from 'fs-extra'
import { join } from 'path' import { dirname, join } from 'path'
import { nextBuild } from 'next-test-utils' import { nextBuild } from 'next-test-utils'
const appDir = join(__dirname, '..') const appDir = join(__dirname, '..')
const pagesDir = join(appDir, 'pages') const pagesDir = join(appDir, 'pages')
const testPage = join(pagesDir, 'test.js')
const writePage = async (content) => { const writePage = async (content, testPage = join(pagesDir, 'test.js')) => {
await fs.ensureDir(pagesDir) await fs.ensureDir(dirname(testPage))
await fs.writeFile(testPage, content) await fs.writeFile(testPage, content)
} }
...@@ -107,4 +106,64 @@ describe('GSP build errors', () => { ...@@ -107,4 +106,64 @@ describe('GSP build errors', () => {
expect(code).toBe(1) expect(code).toBe(1)
expect(stderr).toContain('a string error') expect(stderr).toContain('a string error')
}) })
it('should handle non-serializable error in getStaticProps', async () => {
await writePage(`
export function getStaticProps() {
const err = new Error('my custom error')
err.hello = 'world'
err.a = [1,2,3]
err.original = err
err.b = err.a
throw err
return {
props: {}
}
}
export default function () {
return null
}
`)
const { stderr, code } = await nextBuild(appDir, [], { stderr: true })
expect(code).toBe(1)
expect(stderr).toContain('my custom error')
})
it('should handle non-serializable error in getStaticPaths', async () => {
await writePage(
`
export function getStaticProps() {
return {
props: {}
}
}
export function getStaticPaths() {
const err = new Error('my custom error')
err.hello = 'world'
err.a = [1,2,3]
err.original = err
err.b = err.a
throw err
return {
paths: [],
fallback: true
}
}
export default function () {
return null
}
`,
join(pagesDir, '[slug].js')
)
const { stderr, code } = await nextBuild(appDir, [], { stderr: true })
expect(code).toBe(1)
expect(stderr).toContain('my custom error')
})
}) })
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