question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to catch and handle errors to report logs on server side

See original GitHub issue

Hi, I’m in the situation where we want to sent errors, both on server and client side, to Sentry tool.

Our app uses Express as a custom server. Basically we create an express app, apply some middlewares but delegate all the real job to the next.js handle:

  const app = nextJs({ dev: process.env.NODE_ENV !== 'production' });
  const handler = routes.getRequestHandler(app);
  const expressApp = express();

  ...
  ...

  expressApp.use(morgan('combined', { stream: logger.stream }));
  expressApp.use(statsdMiddleware);

  // Add security
  expressApp.use(helmet());

  // Sentry handler
  expressApp.use(sentry.requestHandler());

  // Load locale and translation messages
  expressApp.use(i18n);

  // Next.js handler
  expressApp.use(handler);

  // Sentry error handler.
  // MUST be placed before any other express error handler !!!
  expressApp.use(sentry.errorHandler());

With this approach next.js takes control over the rendering process and any error is catch by next.js and the only way I have to process it is overriding the _error.js page file.

Within that _error.js file I need a universal way to report errors to Sentry. Currently there are two libraries (raven for node and raven-js por javascript). The proble is I can’t import both of them because raven works for SSR but fails when webpack builds the bundle, and also raven-js fails due XMLHTTPRequest dependency too.

Is there a way I can be notified for next.js error on server side?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:74
  • Comments:97 (34 by maintainers)

github_iconTop GitHub Comments

22reactions
timneutkenscommented, Oct 18, 2017

introduce a hook in that catch block in client/index.js for handling this kind of error? transmit the error to an onerror/onunhandledrejection handler, if any is detected?

This is something we’ve talked about internally. And we’ll be addressing soon.

19reactions
ghostcommented, Dec 22, 2017

Gotta love unortodox solutions

function installErrorHandler(app) {
  const _renderErrorToHTML = app.renderErrorToHTML.bind(app)
  const errorHandler = rollbar.errorHandler()

  app.renderErrorToHTML = (err, req, res, pathname, query) => {
    if (err) {
      errorHandler(err, req, res, () => {})
    }

    return _renderErrorToHTML(err, req, res, pathname, query)
  }

  return app
}
// ¯\_(ツ)_/¯
Read more comments on GitHub >

github_iconTop Results From Across the Web

Logging and error management best practices in SSR apps
Handling SSR errors on the server side · Catching errors in data fetchers · Error status codes · Redirect to error pages ·...
Read more >
Handle and Log Server-Side Errors in ASP.NET Core
Catch Connection Errors​​ Use the DashboardConfigurator. ConnectionError event to catch and log connection errors. The ConnectionError event is ...
Read more >
Best Practices for Client-Side Logging and Error Handling in ...
First, start by creating a separate file such as “logger.js” to initialize a new instance of the Loggly tracker. Then, use this logger...
Read more >
Reporting Services Log Files and Sources - SQL
The HTTP log file contains a record of all HTTP requests and responses handled by the Report Server Web service. Windows Application Log,...
Read more >
React Error Handling and Logging Best Practices
You can use a third-party client-side logging service to log errors persistently. The following are some of the best logging service providers ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found