mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
This adds generating a static 500 status page when a `pages/500.js` file is added similar to how we handle generating static 404 pages when `pages/404.js` is present. This allows showing a customized error page when a 500 error occurs in an optimal way.
24 lines
579 B
JavaScript
24 lines
579 B
JavaScript
import React from 'react'
|
|
|
|
class Error extends React.Component {
|
|
static async getInitialProps({ req, res, err }) {
|
|
if (!req.url.startsWith('/404') && !req.url.startsWith('/500')) {
|
|
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
|