Skip to content

Documentation suggestion: warn about using onFatalError #2796

@OliverJAsh

Description

@OliverJAsh

Package + Version

  • @sentry/browser
  • @sentry/node
  • raven-js
  • raven-node (raven for node)
  • other:

Version:

5.15.5

Description

For the longest time I've been passing an onFatalError option where I do something along the lines of what Raven used to do:

https://github.com/getsentry/raven-node/blob/56a588e3deab4494c6f41e14ed906f3d40511781/lib/client.js#L86-L89

    this.onFatalError = this.defaultOnFatalError = function (err, sendErr, eventId) {
      console.error(err && err.stack ? err.stack : err);
      process.exit(1);
    };

(For context, the only reason I did this was just because I wanted to prefix the logged error message with "fatal error", to differentiate them from non-fatal errors we choose to log.)

However, just today I noticed that uncaught exceptions were not being reported to Sentry. After some debugging it seemed to be because of my custom onFatalError. In the latest version of @sentry/node, the default onFatalError only kills the process when all events have finished sending:

let onFatalError: onFatalErrorHandlerType = logAndExitProcess;

export function logAndExitProcess(error: Error): void {
console.error(error && error.stack ? error.stack : error);
const client = getCurrentHub().getClient<NodeClient>();
if (client === undefined) {
logger.warn('No NodeClient was defined, we are exiting the process now.');
global.process.exit(1);
return;
}
const options = client.getOptions();
const timeout =
(options && options.shutdownTimeout && options.shutdownTimeout > 0 && options.shutdownTimeout) ||
DEFAULT_SHUTDOWN_TIMEOUT;
forget(
client.close(timeout).then((result: boolean) => {
if (!result) {
logger.warn('We reached the timeout for emptying the request buffer, still exiting now!');
}
global.process.exit(1);
}),
);
}

By passing a custom onFatalError I was unintentionally overriding this behaviour, thus the event never reached Sentry because the process was killed before the event was sent.

For this reason, I think the docs for onFatalError should carry a big warning—that by customising this functionality, you lose this important facility. WDYT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions