Skip to content

weird behaviour from uncaughtExceptionMonitor for unhandled rejections #35291

Closed
@mlarcher

Description

What steps will reproduce the bug?

process.on('uncaughtExceptionMonitor', (err, origin) => {
  console.log('uncaughtExceptionMonitor handler', origin);
});

async function uncaughtPromise() {
  throw new Error('yo');
}

uncaughtPromise();

What is the expected behavior?

I should see the uncaughtExceptionMonitor handler

What do you see instead?

(node:39) UnhandledPromiseRejectionWarning: Error: yo
    at uncaughtPromise (/data/tmp.js:6:9)
    at Object.<anonymous> (/data/tmp.js:9:1)
    at Module._compile (internal/modules/cjs/loader.js:1176:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:39) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:39) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Additional information

If I add a handler for unhandledRejection that just re-throws the error like so:

process.on('unhandledRejection', (err) => {
  console.log('unhandledRejection handler‘)
  throw err;
});

process.on('uncaughtExceptionMonitor', (err, origin) => {
  console.log('uncaughtExceptionMonitor handler', origin);
});

async function uncaughtPromise() {
  throw new Error('yo');
}

uncaughtPromise();

then the uncaughtExceptionMonitor is reached :

unhandledRejection handler
uncaughtExceptionMonitor handler uncaughtException
/data/tmp.js:3
  throw err;
  ^

Error: yo
    at uncaughtPromise (/data/tmp.js:11:9)
    at Object.<anonymous> (/data/tmp.js:14:1)
    at Module._compile (internal/modules/cjs/loader.js:1176:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

(but that is now an uncaughtException and not an unhandledRejection anymore, when I come to think about it !)

Also, an exception not related to unhandled promise works as expected, i.e.:

process.on('uncaughtExceptionMonitor', (err, origin) => {
  console.log('uncaughtExceptionMonitor handler', origin);
});

unknownFunction();

gives me the expected

uncaughtExceptionMonitor handler uncaughtException
/data/tmp.js:5
unknownFunction();
^

ReferenceError: unknownFunction is not defined
    at Object.<anonymous> (/data/tmp.js:5:1)
    at Module._compile (internal/modules/cjs/loader.js:1176:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1196:10)
    at Module.load (internal/modules/cjs/loader.js:1040:32)
    at Function.Module._load (internal/modules/cjs/loader.js:929:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

So basically it seems to me that uncaughtExceptionMonitor doesn't work for unhandledRejection 🤷

Activity

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

Metadata

Assignees

No one assigned

    Labels

    processIssues and PRs related to the process subsystem.questionIssues that look for answers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions