next.js: _error.js getInitialProps is not called for an error thrown at the top level of a module

Bug report

Describe the bug

Error.getInitialProps is not called when an exception is thrown at the top level of a page while it is being loaded. This is problematic because getInitialProps would have provided access to the Error object. I want the Error so that I can log it and potentially display it to the user in render().

To Reproduce

I’ve created a minimal reproduction here: https://github.com/WestonThayer/bug-nextjs-error-getinitialprops

Expected behavior

The Error that was thrown should be made available to _error.js. I would expect to receive it via getInitialProps, but I supposed having it as a prop passed in to render would work too.

Additional context

I think this was introduced by https://github.com/zeit/next.js/pull/4764, specifically the lines that use the props from the server. I can see how that makes sense if the Error was thrown on the server side and _error.js’s getInitialProps ran on the server, but it doesn’t account for code that works fine on the server, but throws on the client.

As a workaround, the exception is passed as a prop to App, so I’m simply passing it on:

class MyApp extends App {
  render() {
    const { Component, pageProps, err } = this.props;
    return <Component {...pageProps} err={err} />;
  }
}

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 49
  • Comments: 22 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Hi @WestonThayer I am stuck on trying to write a test for this in my app to check if can correctly catch all errors. In your sample repo, can you please explain more precisely what error will be produced? If you were to rewrite with an explicit throw new Error, where would you put it? Something like this for instance?

import React from "react";
import Head from "next/head";

if (typeof window !== "undefined") {
  throw new Error(
    "Error thrown during top-level page rendering client-side (if you see this message in the ErrorBoundary, everything works as expected)."
  );
}
const Home = () => {
  return (
      <div>Hello</div>
    </div>
  );
};
export default Home;

I can’t catch this properly using _error when opening this page but maybe I am doing smth wrong.

Edit: I think I got it: _error is only activated in production as explained in the doc. You cannot test this with dev, you need to build and start the app to test this behaviour. The “hack” works ok when I build the app.

Edit2: it seems to work but I have a discrepancy between SSR and client-render with this example. Server-side, I get a loader (which is expected when there is no failure). Client side, I get the error message.

error-turn-around

Result is kinda funny: my error turns around because there’s a mix between the circular progress loader (from the server-render) and the error render client-side. Did some of you encounter reconciliation issue in this scenario?

Trying to use @WestonThayer 's workaround with Typescript results in an error:

function App({ Component, pageProps, err }: AppProps) { 
  // rest of app here
})

Error:

Property 'err' does not exist on type 'AppPropsType<Router, {}>

What would be the right types to use in this case?

Just noting that this still repro’s with next@9.3.4.

@antkougblenou It might be unrelated, but 10 days ago Sentry has released official npm package to support Next.js projects and it might help with the setup. I was in the same scenario as you, building with a custom implementation of Sentry for next.js but found that it’s not worthy.

npm install --save @sentry/nextjs

Unfortunately @kachar I am well aware of that package, but since the integration of sentry will be in a production project I cannot use a package that is interacting with experimental packages under the hood.

For anyone out there that is fine with this @sentry/nextjs is indeed a good alternative.

Same issue here, and it appears that the workaround of passing err to <Component> is not working. I’m on next@10.0.6.

Edit: I was able to resolve my issue with this solution: https://github.com/isaachinman/next-i18next/issues/615#issuecomment-575578375

Slightly off topic but I hope you guys can help - this is driving me nuts.

I’ve added the code from with-sentry example with the workaround and everything.

If I build locally and start, the server errors are logged.

When I push and the project is built on Vercel, no server side errors logged to Sentry. I checked that the Sentry DSN is correct by printing it to console. The sourcemaps are uploaded also.

Any ideas?

EDIT: I deployed the example verbatim and server-side error tracking doesn’t work there either. 😕

@fredrik-sogaard it’s not a publicly exposed part of AppProps, thus my workaround is taking advantage of non-public parts of Next.js’s API and should be considered brittle.

But, it works for now. I’m just casting to any: const err = (props as any).err;.

Sorry, I can see how I wasn’t specific enough. The goal is to get This will not log, because it was not run to display in the browser console. The only reason you saw that text in the terminal is because the browser attempted to load /favicon.ico and it wasn’t found, which yielded a 404, so the server rendered _error.js. It’s unrelated to the bug.

I’ve updated the repo to add a link to the favicon in <head>, so you won’t see that log in the terminal anymore.

@eric-burel sounds like you figured it out, building production is the way to test it. Is the progress loader something you added? Your example page jsx above should cause Next.js to build a static page, it shouldn’t need to SSR that.