Skip to content

Expose the "enhanced" stack trace from uncaught exceptions to the uncaughtException and uncaughtExceptionMonitor handlers on process #55838

Description

@danfuzz

What is the problem this feature will solve?

In my app, I have a logging system which writes entries to files and/or the network in a machine-parsable form. I use the uncaughtException handler on process in order to get uncaught exceptions written out to this logging system. I noticed that if there's no uncaughtException handler, Node will print out an extra stack trace to the console indicating the call site of the <emitter>.emit('error', ...) call which emitted the ultimately-uncaught exception. However, it seems like there's no programmatic way for me to get that extra trace in my own handler.

This feature will solve the problem of being able to programmatically extract this extra — and quite useful! — information in a way that works well with a system that eschews the console.

What is the feature you are proposing to solve the problem?

I don't know what tactic would work best within the larger Node ecosystem, but as a straw-man suggestion:

Add a third argument to the uncaughtException and uncaughtExceptionMonitor callbacks when an "enhanced" error is available, containing information corresponding to the site and target of the emit() call. It could, for example, be a plain object containing an Error representing the call site and an object reference to the emitter itself. But if that's too dangerous, it might instead have strings representing that info. For example (in the former case), the following would nearly duplicate what the current default uncaughtException handler prints out to the console:

process.on('uncaughtException', (err, origin, emitInfo) => {
  console.log('%s', err.stack);
  if (emitInfo) {
    const { emitter, emitSite } = emitInfo;
    console.log(`Emitted 'error' event of ${emitter.constructor.name} instance at:\n%s`,
      emitSite.stack);
  }
});

What alternatives have you considered?

Having a separate process to scrape console output to find things that look like uncaught exceptions, so I can push them into the structured logging system. (Oof.)

Activity

  1. danfuzz commented on Nov 13, 2024

    @danfuzz
    Author

    Source reference for the current console behavior: https://github.com/nodejs/node/blob/main/lib/events.js#L422

  2. added
    eventsIssues and PRs related to EventEmitter and the events module.
    on Nov 13, 2024
  3. github-actions commented on May 13, 2025

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-stale Issues and PRs exempt from automated stale handling. label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
    For more information on how the project manages feature requests, please consult the feature request management document.

  4. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on May 13, 2025
  5. danfuzz commented on May 13, 2025

    @danfuzz
    Author

    Commenting here to keep the issue open. I think this issue is worth addressing.

  6. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on May 14, 2025
  7. github-actions commented on Nov 10, 2025

    @github-actions
    Contributor

    There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-stale Issues and PRs exempt from automated stale handling. label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
    For more information on how the project manages feature requests, please consult the feature request management document.

  8. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Nov 10, 2025
  9. danfuzz commented on Nov 10, 2025

    @danfuzz
    Author

    Commenting here to keep the issue open. I think this issue is worth addressing.

  10. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Nov 11, 2025
  11. github-actions commented on Jun 9, 2026

    @github-actions
    Contributor

    This issue has been marked as stale due to 210 days of inactivity.
    It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

  12. added
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jun 9, 2026
  13. danfuzz commented on Jun 10, 2026

    @danfuzz
    Author

    Commenting here to keep the issue open. I think this issue is worth addressing.

  14. removed
    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.
    on Jun 11, 2026
  15. santusht06 commented on Aug 27, 2026

    @santusht06

    I've opened a fix for this in #65580. Running fatalExceptionStackEnhancers.beforeInspector(er) before emitting uncaughtExceptionMonitor and uncaughtException ensures that the enhanced stack trace (with the emitter call site) is accessible to handlers.

  16. danfuzz commented on Aug 27, 2026

    @danfuzz
    Author

    @santusht06 You're my hero.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    eventsIssues and PRs related to EventEmitter and the events module.feature requestIssues requesting new Node.js features.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions