Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve node OnUncaughtException integration #6146

Closed
mydea opened this issue Nov 7, 2022 · 4 comments
Closed

Improve node OnUncaughtException integration #6146

mydea opened this issue Nov 7, 2022 · 4 comments

Comments

@mydea
Copy link
Member

mydea commented Nov 7, 2022

Problem Statement

The current integration configuration for the node OnUncaughtExceptionOptions exception can be a bit confusing, especially with recently necessary changes regarding the default behavior.

Current behavior:

  • Following the node.js default, we exit the process on uncaught exceptions
  • For e.g. next.js, we need the capability to opt out of this behavior - however, we only want to opt out of it if any other handler has been registered.
  • Then there is also the option of providing an onFatalError handler to conditionally define if the process should exit or not

Solution Brainstorm

We could add a new option to (eventually) replace all existing options, e.g. mode.

A possible API could be:

type OnUncaughtExceptionMode = 'exit' | 'continue' | (firstError: Error, secondError?: Error) => boolean;

// default, matches node.js
new OnUncaughtException({
  onException: 'exit'
});

new OnUncaughtException({
  onException: 'continue'
});

new OnUncaughtException({
  onException: (error) => {
    // do something
    // return `true` to exit the process, any other return value will keep it running
    return true;
  }
});

With the behaviour:

  • exit: Always exit the process on uncaught exceptions. The default.
  • continue: Do not exit the process. Could be set by the next.js integration.
  • a function: Leave this to the user. Can eventually replace onFatalError
@mydea
Copy link
Member Author

mydea commented Nov 9, 2022

So I think the following exception handler would kind of cover all relevant cases for now:

const onExceptionHandler = (error: Error): void => {
  const { onException } = this._options;
  const hub = getCurrentHub();

  if (hub.getIntegration(OnUncaughtException)) {
    hub.withScope((scope: Scope) => {
      scope.setLevel('fatal');
      hub.captureException(error, {
        originalException: error,
        data: { mechanism: { handled: false, type: 'onuncaughtexception' } },
      });
    });
  }

  if (onException === 'exit') {
    logAndExitProcess(error);
    return;
  }

  if (onException === 'warn') {
   logger.warn(`onUncaughtException integration encountered an exception: ${error}`);
    return;
  }

  if (typeof onException === 'function') {
    const result = onException(error);

    if (result !== undefined) {
      logAndExitProcess(result);
    }
    return;
  }

  // else: 'continue'
};

My idea would be to, for now, allow to opt-in to that behavior by setting onException on the integration. If that's set, use the new logic, else, the "old" one.

Then we can think about deprecating the "old" functionality, and flipping defaults for v8. The trickiest migration/thing is the onFatalError stuff, not sure if we want to/need to keep support for that (without any changes) in the future.

Thoughts, @lobsterkatie ?

We could then remove (or deprecate) the exitEvenIfOtherHandlersAreRegistered and instead set onException: 'continue' in the nextjs integration.

@Lms24 Lms24 added this to the 8.0.0 milestone Nov 14, 2022
@Nikoms
Copy link

Nikoms commented Dec 13, 2022

I agree that, by default, it shouldn't call exit. We just updated the library and took a day to understand why our servers were killed 😅.

@AbhiPrasad
Copy link
Member

Can this be closed now @lforst?

@lforst
Copy link
Member

lforst commented Apr 25, 2024

Yes! Thanks for reminding me.

@lforst lforst closed this as completed Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants