Skip to content

Instantly share code, notes, and snippets.

@CharlyJazz
Created March 29, 2019 19:30
Show Gist options
  • Save CharlyJazz/4a3573c923e8de43192aef43461d6302 to your computer and use it in GitHub Desktop.
Save CharlyJazz/4a3573c923e8de43192aef43461d6302 to your computer and use it in GitHub Desktop.
import React from 'react'
import ErrorPage from 'next/error'
export default Component => {
return class WithError extends React.Component {
static async getInitialProps(ctx) {
const props =
(Component.getInitialProps
? await Component.getInitialProps(ctx)
: null) || {}
if (props.statusCode && ctx.res) {
ctx.res.statusCode = props.statusCode
}
return props
}
render() {
if (this.props.statusCode) {
return <ErrorPage statusCode={this.props.statusCode} />
}
return <Component {...this.props} />
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment