2018-10-03 00:08:57 +02:00
|
|
|
import React from 'react'
|
2019-09-04 16:00:54 +02:00
|
|
|
import Head from '../next-server/lib/head'
|
|
|
|
import { NextPageContext } from '../next-server/lib/utils'
|
2018-10-03 00:08:57 +02:00
|
|
|
|
2019-04-22 19:55:03 +02:00
|
|
|
const statusCodes: { [code: number]: string } = {
|
2019-02-22 16:25:11 +01:00
|
|
|
400: 'Bad Request',
|
|
|
|
404: 'This page could not be found',
|
2019-07-04 10:22:25 +02:00
|
|
|
405: 'Method Not Allowed',
|
2019-02-22 16:25:11 +01:00
|
|
|
500: 'Internal Server Error',
|
|
|
|
}
|
|
|
|
|
2019-04-26 09:37:57 +02:00
|
|
|
export type ErrorProps = {
|
2019-05-30 03:19:32 +02:00
|
|
|
statusCode: number
|
2019-07-10 07:53:20 +02:00
|
|
|
title?: string
|
2019-04-22 19:55:03 +02:00
|
|
|
}
|
|
|
|
|
2020-03-14 09:58:20 +01:00
|
|
|
function _getInitialProps({
|
|
|
|
res,
|
|
|
|
err,
|
|
|
|
}: NextPageContext): Promise<ErrorProps> | ErrorProps {
|
|
|
|
const statusCode =
|
|
|
|
res && res.statusCode ? res.statusCode : err ? err.statusCode! : 404
|
|
|
|
return { statusCode }
|
|
|
|
}
|
|
|
|
|
2019-05-23 21:31:22 +02:00
|
|
|
/**
|
|
|
|
* `Error` component used for handling errors.
|
|
|
|
*/
|
2019-04-26 09:37:57 +02:00
|
|
|
export default class Error<P = {}> extends React.Component<P & ErrorProps> {
|
2019-01-11 00:30:01 +01:00
|
|
|
static displayName = 'ErrorPage'
|
|
|
|
|
2020-03-14 09:58:20 +01:00
|
|
|
static getInitialProps = _getInitialProps
|
|
|
|
static origGetInitialProps = _getInitialProps
|
2018-10-03 00:08:57 +02:00
|
|
|
|
2019-04-22 19:55:03 +02:00
|
|
|
render() {
|
2018-10-03 00:08:57 +02:00
|
|
|
const { statusCode } = this.props
|
2019-05-30 03:19:32 +02:00
|
|
|
const title =
|
|
|
|
this.props.title ||
|
|
|
|
statusCodes[statusCode] ||
|
|
|
|
'An unexpected error has occurred'
|
2018-10-03 00:08:57 +02:00
|
|
|
|
2019-02-13 03:32:25 +01:00
|
|
|
return (
|
|
|
|
<div style={styles.error}>
|
|
|
|
<Head>
|
|
|
|
<title>
|
|
|
|
{statusCode}: {title}
|
|
|
|
</title>
|
|
|
|
</Head>
|
|
|
|
<div>
|
|
|
|
<style dangerouslySetInnerHTML={{ __html: 'body { margin: 0 }' }} />
|
|
|
|
{statusCode ? <h1 style={styles.h1}>{statusCode}</h1> : null}
|
|
|
|
<div style={styles.desc}>
|
|
|
|
<h2 style={styles.h2}>{title}.</h2>
|
|
|
|
</div>
|
2018-10-03 00:08:57 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2019-02-13 03:32:25 +01:00
|
|
|
)
|
2018-10-03 00:08:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-22 19:55:03 +02:00
|
|
|
const styles: { [k: string]: React.CSSProperties } = {
|
2018-10-03 00:08:57 +02:00
|
|
|
error: {
|
|
|
|
color: '#000',
|
|
|
|
background: '#fff',
|
2019-02-13 03:32:25 +01:00
|
|
|
fontFamily:
|
|
|
|
'-apple-system, BlinkMacSystemFont, Roboto, "Segoe UI", "Fira Sans", Avenir, "Helvetica Neue", "Lucida Grande", sans-serif',
|
2018-10-03 00:08:57 +02:00
|
|
|
height: '100vh',
|
|
|
|
textAlign: 'center',
|
|
|
|
display: 'flex',
|
|
|
|
flexDirection: 'column',
|
|
|
|
alignItems: 'center',
|
2019-04-22 19:55:03 +02:00
|
|
|
justifyContent: 'center',
|
2018-10-03 00:08:57 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
desc: {
|
|
|
|
display: 'inline-block',
|
|
|
|
textAlign: 'left',
|
|
|
|
lineHeight: '49px',
|
|
|
|
height: '49px',
|
2019-04-22 19:55:03 +02:00
|
|
|
verticalAlign: 'middle',
|
2018-10-03 00:08:57 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
h1: {
|
|
|
|
display: 'inline-block',
|
|
|
|
borderRight: '1px solid rgba(0, 0, 0,.3)',
|
|
|
|
margin: 0,
|
|
|
|
marginRight: '20px',
|
|
|
|
padding: '10px 23px 10px 0',
|
|
|
|
fontSize: '24px',
|
|
|
|
fontWeight: 500,
|
2019-04-22 19:55:03 +02:00
|
|
|
verticalAlign: 'top',
|
2018-10-03 00:08:57 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
h2: {
|
|
|
|
fontSize: '14px',
|
|
|
|
fontWeight: 'normal',
|
|
|
|
lineHeight: 'inherit',
|
|
|
|
margin: 0,
|
2019-04-22 19:55:03 +02:00
|
|
|
padding: 0,
|
|
|
|
},
|
2018-10-03 00:08:57 +02:00
|
|
|
}
|