Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure error is passed up in minimal mode #22030

Merged
merged 1 commit into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export default class Server {
this.nextConfig = loadConfig(phase, this.dir, conf)
this.distDir = join(this.dir, this.nextConfig.distDir)
this.publicDir = join(this.dir, CLIENT_PUBLIC_FILES_PATH)
this.hasStaticDir = fs.existsSync(join(this.dir, 'static'))
this.hasStaticDir = !minimalMode && fs.existsSync(join(this.dir, 'static'))

// Only serverRuntimeConfig needs the default
// publicRuntimeConfig gets it's default in client/index.js
Expand Down Expand Up @@ -567,6 +567,9 @@ export default class Server {
try {
return await this.run(req, res, parsedUrl)
} catch (err) {
if (this.minimalMode) {
throw err
}
this.logError(err)
res.statusCode = 500
res.end('Internal Server Error')
Expand Down Expand Up @@ -1835,14 +1838,19 @@ export default class Server {
}
}
} catch (err) {
this.logError(err)

if (err && err.code === 'DECODE_FAILED') {
this.logError(err)
res.statusCode = 400
return await this.renderErrorToHTML(err, req, res, pathname, query)
}
res.statusCode = 500
return await this.renderErrorToHTML(err, req, res, pathname, query)
const html = await this.renderErrorToHTML(err, req, res, pathname, query)

if (this.minimalMode) {
throw err
}
this.logError(err)
return html
}
res.statusCode = 404
return await this.renderErrorToHTML(null, req, res, pathname, query)
Expand All @@ -1863,6 +1871,10 @@ export default class Server {
)
}
const html = await this.renderErrorToHTML(err, req, res, pathname, query)

if (this.minimalMode && res.statusCode === 500) {
throw err
}
if (html === null) {
return
}
Expand Down Expand Up @@ -2007,9 +2019,11 @@ export default class Server {
}

let nextFilesStatic: string[] = []
nextFilesStatic = recursiveReadDirSync(
join(this.distDir, 'static')
).map((f) => join('.', relative(this.dir, this.distDir), 'static', f))
nextFilesStatic = !this.minimalMode
? recursiveReadDirSync(join(this.distDir, 'static')).map((f) =>
join('.', relative(this.dir, this.distDir), 'static', f)
)
: []

return (this._validFilesystemPathSet = new Set<string>([
...nextFilesStatic,
Expand Down
14 changes: 14 additions & 0 deletions test/integration/required-server-files/pages/errors/gip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function Page(props) {
return <p>here comes an error</p>
}

Page.getInitialProps = ({ query }) => {
if (query.crash) {
throw new Error('gip hit an oops')
}
return {
hello: 'world',
}
}

export default Page
28 changes: 28 additions & 0 deletions test/integration/required-server-files/pages/errors/gsp/[post].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useRouter } from 'next/router'

function Page(props) {
if (useRouter().isFallback) {
return <p>loading...</p>
}
return <p>here comes an error</p>
}

export const getStaticPaths = () => {
return {
paths: [],
fallback: true,
}
}

export const getStaticProps = ({ params }) => {
if (params.post === 'crash') {
throw new Error('gsp hit an oops')
}
return {
props: {
hello: 'world',
},
}
}

export default Page
16 changes: 16 additions & 0 deletions test/integration/required-server-files/pages/errors/gssp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function Page(props) {
return <p>here comes an error</p>
}

export const getServerSideProps = ({ query }) => {
if (query.crash) {
throw new Error('gssp hit an oops')
}
return {
props: {
hello: 'world',
},
}
}

export default Page
31 changes: 30 additions & 1 deletion test/integration/required-server-files/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ let nextApp
let appPort
let buildId
let requiredFilesManifest
let errors = []

describe('Required Server Files', () => {
beforeAll(async () => {
Expand Down Expand Up @@ -58,7 +59,8 @@ describe('Required Server Files', () => {
try {
await nextApp.getRequestHandler()(req, res)
} catch (err) {
console.error(err)
console.error('top-level', err)
errors.push(err)
res.statusCode = 500
res.end('error')
}
Expand Down Expand Up @@ -419,4 +421,31 @@ describe('Required Server Files', () => {
path: ['hello', 'world'],
})
})

it('should bubble error correctly for gip page', async () => {
errors = []
const res = await fetchViaHTTP(appPort, '/errors/gip', { crash: '1' })
expect(res.status).toBe(500)
expect(await res.text()).toBe('error')
expect(errors.length).toBe(1)
expect(errors[0].message).toContain('gip hit an oops')
})

it('should bubble error correctly for gssp page', async () => {
errors = []
const res = await fetchViaHTTP(appPort, '/errors/gssp', { crash: '1' })
expect(res.status).toBe(500)
expect(await res.text()).toBe('error')
expect(errors.length).toBe(1)
expect(errors[0].message).toContain('gssp hit an oops')
})

it('should bubble error correctly for gsp page', async () => {
errors = []
const res = await fetchViaHTTP(appPort, '/errors/gsp/crash')
expect(res.status).toBe(500)
expect(await res.text()).toBe('error')
expect(errors.length).toBe(1)
expect(errors[0].message).toContain('gsp hit an oops')
})
})