Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
c39c5d26
Commit
c39c5d26
authored
Jun 19, 2019
by
JJ Kasper
Committed by
Joe Haddad
Jun 19, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add error handling for `_error` page (#7608)
parent
69150043
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
89 additions
and
4 deletions
+89
-4
next-server.ts
packages/next-server/server/next-server.ts
+19
-4
_error.js
test/integration/error-in-error/pages/_error.js
+21
-0
index.js
test/integration/error-in-error/pages/index.js
+10
-0
index.test.js
test/integration/error-in-error/test/index.test.js
+39
-0
No files found.
packages/next-server/server/next-server.ts
View file @
c39c5d26
...
...
@@ -582,10 +582,25 @@ export default class Server {
query
:
ParsedUrlQuery
=
{}
)
{
const
result
=
await
this
.
findPageComponents
(
'/_error'
,
query
)
return
this
.
renderToHTMLWithComponents
(
req
,
res
,
'/_error'
,
query
,
result
,
{
...
this
.
renderOpts
,
err
,
})
let
html
try
{
html
=
await
this
.
renderToHTMLWithComponents
(
req
,
res
,
'/_error'
,
query
,
result
,
{
...
this
.
renderOpts
,
err
,
}
)
}
catch
(
err
)
{
console
.
error
(
err
)
res
.
statusCode
=
500
html
=
'Internal Server Error'
}
return
html
}
public
async
render404
(
...
...
test/integration/error-in-error/pages/_error.js
0 → 100644
View file @
c39c5d26
import
React
from
'react'
class
Error
extends
React
.
Component
{
static
async
getInitialProps
({
res
,
err
})
{
await
Promise
.
reject
(
new
Error
(
'an error in error'
))
const
statusCode
=
res
?
res
.
statusCode
:
err
?
err
.
statusCode
:
null
return
{
statusCode
}
}
render
()
{
return
(
<
p
>
{
this
.
props
.
statusCode
?
`An error
${
this
.
props
.
statusCode
}
occurred on server`
:
'An error occurred on client'
}
<
/p
>
)
}
}
export
default
Error
test/integration/error-in-error/pages/index.js
0 → 100644
View file @
c39c5d26
import
Link
from
'next/link'
export
default
()
=>
(
<>
<
h3
>
Hi
👋
<
/h3
>
<
Link
href
=
'/a-non-existing-page'
>
<
a
>
a
lnik
to
no
-
where
<
/a
>
<
/Link
>
<
/
>
)
test/integration/error-in-error/test/index.test.js
0 → 100644
View file @
c39c5d26
/* eslint-env jest */
/* global jasmine */
import
path
from
'path'
import
webdriver
from
'next-webdriver'
import
{
nextBuild
,
nextStart
,
findPort
,
killApp
,
renderViaHTTP
,
waitFor
}
from
'next-test-utils'
jasmine
.
DEFAULT_TIMEOUT_INTERVAL
=
1000
*
60
*
1
const
appDir
=
path
.
join
(
__dirname
,
'..'
)
let
app
let
port
describe
(
'Handles an Error in _error'
,
()
=>
{
beforeAll
(
async
()
=>
{
await
nextBuild
(
appDir
)
port
=
await
findPort
()
app
=
await
nextStart
(
appDir
,
port
)
})
afterAll
(()
=>
killApp
(
app
))
it
(
'Handles error during SSR'
,
async
()
=>
{
const
html
=
await
renderViaHTTP
(
port
,
'/some-404-page'
)
expect
(
html
).
toMatch
(
/internal server error/i
)
})
it
(
'Handles error during client transition'
,
async
()
=>
{
const
browser
=
await
webdriver
(
port
,
'/'
)
await
browser
.
elementByCss
(
'a'
).
click
()
await
waitFor
(
1000
)
const
html
=
await
browser
.
eval
(
'document.body.innerHTML'
)
expect
(
html
).
toMatch
(
/internal server error/i
)
})
})
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment