Skip to content

Commit

Permalink
Fix(app renderer): set status code to 500 if unexpected error occurs …
Browse files Browse the repository at this point in the history
…before streaming
  • Loading branch information
dpnolte committed Sep 30, 2023
1 parent a623685 commit bee4a54
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,10 @@ export const renderToHTMLOrFlight: AppPageRender = (

const is404 = res.statusCode === 404

if (!is404 && !hasRedirectError) {
res.statusCode = 500
}

// Preserve the existing RSC inline chunks from the page rendering.
// To avoid the same stream being operated twice, clone the origin stream for error rendering.
const serverErrorComponentsRenderOpts: typeof serverComponentsRenderOpts =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const revalidate = 0

export default function UnexpectedErrorPage(props) {
// use query param to only throw error during runtime, not build time
if (props.searchParams.error) {
throw new Error('Oh no')
}
return (
<>
<p id="page">/unexpected-error</p>
</>
)
}
7 changes: 7 additions & 0 deletions test/production/app-dir/unexpected-error/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Loading() {
return <p>loading</p>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function UnexpectedErrorPage(props) {
// use query param to only throw error during runtime, not build time
if (props.searchParams.error) {
throw new Error('Oh no')
}
return (
<>
<p id="page">/unexpected-error</p>
</>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function UnexpectedErrorPage(props) {
// use query param to only throw error during runtime, not build time
if (props.searchParams.error) {
throw new Error('Oh no')
}
return (
<>
<p id="page">/unexpected-error</p>
</>
)
}
6 changes: 6 additions & 0 deletions test/production/app-dir/unexpected-error/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}

module.exports = nextConfig
26 changes: 26 additions & 0 deletions test/production/app-dir/unexpected-error/unexpected-error.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { createNextDescribe } from 'e2e-utils'

createNextDescribe(
'unexpected-error',
{
files: __dirname,
},
({ next }) => {
it('should set response status to 500 for unexpected errors in ssr app route', async () => {
const res = await next.fetch('/ssr-unexpected-error?error=true')
expect(res.status).toBe(500)
})

it('cannot change response status when streaming has started', async () => {
const res = await next.fetch(
'/ssr-unexpected-error-after-streaming?error=true'
)
expect(res.status).toBe(200)
})

it('should set response status to 500 for unexpected errors in isr app route', async () => {
const res = await next.fetch('/isr-unexpected-error?error=true')
expect(res.status).toBe(500)
})
}
)

0 comments on commit bee4a54

Please sign in to comment.