简体   繁体   中英

NextJS use client-side error in custom error page

NextJS renders the custom error page ( _error.jsx ) when there is client-side UI error. Is it possible to get information on the error inside the custom error page?

I'm using a custom App:

// pages/_app.jsx
import App from 'next/app'

function CustomApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}


CustomApp.getInitialProps = async (props) => {
  { Component, ctx } = props

  const initProps = await Component.getInitialProps(ctx);

  return { pageProps: initProps }
}

export default CustomApp

and I have a page which triggers an error

// pages/throw_error.jsx
function ThrowErrorPage() {
    throw new Error("client-side error")
    ...
}

export default ThrowError

and a custom error page

// pages/_error.jsx
function Error(props) {
  
   // Expecting error from the UI
   const { err } = props;
  
   return (
      <div>{err.message}</div>
   )
}

Error.getInitialProps = async (props) => {
   const { err } = props;
   const statusCode = res ? res.statusCode : err ? err.statusCode : 404;
   return { err, statusCode };
}

export default Error

I was expecting err to be passed from the UI to _error.jsx . Am I missing something?

So it turns out that NextJS does return client-side error to the custom error component. When a client-side error occurs, NextJS fetches the custom _error.jsx [ 1 ], and passes it as the value of the Component to the custom App ( _app.jsx ), along with a ctx object which contains the error object err [ 2 ]. In the custom App, we can then pass the error object to the error component as a prop, and extract it in the component for display/logging:

First pass the error object in the custom app to the component where the error is going to be rendered:

// pages/_app.jsx
import App from 'next/app'

function CustomApp({ Component, pageProps }) {
  return <Component {...pageProps} />
}

CustomApp.getInitialProps = async (props) => {
  { Component, ctx : { err } } = props;
  
  // Return early if there is an error 
  // pass the error to the component
  if (err) return { pageProps : { err } };

  const componentProps = await Component.getInitialProps(ctx);

  return { pageProps: { ...componentProps } };
}

export default CustomApp;

Then extract the error object from the component's prop

// pages/_error.jsx
function Error({ err }) {
   logToService(err)
   return err.message;
}

export default Error;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM