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

feat(node): support uncaughtException and uncaughtExceptionMonitor #2460

Merged
merged 5 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions node/_tools/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,10 @@
"test-events-list.js",
"test-events-on-async-iterator.js",
"test-events-once.js",
"test-events-uncaught-exception-stack.js",
"test-eventtarget-brandcheck.js",
"test-exception-handler.js",
"test-exception-handler2.js",
"test-file-write-stream.js",
"test-file-write-stream2.js",
"test-fs-access.js",
Expand Down Expand Up @@ -288,6 +291,7 @@
"test-fs-write.js",
"test-fs-writev-sync.js",
"test-fs-writev.js",
"test-handle-wrap-close-abort.js",
"test-http-agent-getname.js",
"test-http-client-get-url.js",
"test-http-client-read-in-error.js",
Expand Down Expand Up @@ -415,6 +419,8 @@
"test-process-exit-recursive.js",
"test-process-exit.js",
"test-process-uptime.js",
"test-promise-unhandled-silent.js",
"test-promise-unhandled-throw-handler.js",
"test-punycode.js",
"test-querystring-escape.js",
"test-querystring-maxKeys-non-finite.js",
Expand Down Expand Up @@ -583,9 +589,12 @@
"test-timers-clear-object-does-not-throw-error.js",
"test-timers-clear-timeout-interval-equivalent.js",
"test-timers-clearImmediate.js",
"test-timers-interval-throw.js",
"test-timers-non-integer-delay.js",
"test-timers-same-timeout-wrong-list-deleted.js",
"test-timers-timeout-with-non-integer.js",
"test-timers-uncaught-exception.js",
"test-timers-unref-throw-then-ref.js",
"test-timers-user-call.js",
"test-timers-zero-timeout.js",
"test-url-fileurltopath.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 16.13.0
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually

'use strict';
const common = require('../common');
const assert = require('assert');
const EventEmitter = require('events');

// Tests that the error stack where the exception was thrown is *not* appended.

process.on('uncaughtException', common.mustCall((err) => {
const lines = err.stack.split('\n');
assert.strictEqual(lines[0], 'Error');
lines.slice(1).forEach((line) => {
assert.match(line, /^ at/);
});
}));

new EventEmitter().emit('error', new Error());
47 changes: 47 additions & 0 deletions node/_tools/test/parallel/test-exception-handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 16.13.0
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually

// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
const common = require('../common');
const assert = require('assert');

const MESSAGE = 'catch me if you can';

process.on('uncaughtException', common.mustCall((e) => {
console.log('uncaught exception! 1');
assert.strictEqual(MESSAGE, e.message);
}));

process.on('uncaughtException', common.mustCall((e) => {
console.log('uncaught exception! 2');
assert.strictEqual(MESSAGE, e.message);
}));

setTimeout(() => {
throw new Error(MESSAGE);
}, 10);
43 changes: 43 additions & 0 deletions node/_tools/test/parallel/test-exception-handler2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 16.13.0
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually

// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
const common = require('../common');
const assert = require('assert');

process.on('uncaughtException', function(err) {
console.log(`Caught exception: ${err}`);
});

setTimeout(common.mustCall(function() {
console.log('This will still run.');
}), 50);

// Intentionally cause an exception, but don't catch it.
nonexistentFunc(); // eslint-disable-line no-undef
assert.fail('This will not run.');
44 changes: 44 additions & 0 deletions node/_tools/test/parallel/test-handle-wrap-close-abort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 16.13.0
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually

// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
const common = require('../common');

process.on('uncaughtException', common.mustCall(2));

setTimeout(function() {
process.nextTick(function() {
const c = setInterval(function() {
clearInterval(c);
throw new Error('setInterval');
}, 1);
});
setTimeout(function() {
throw new Error('setTimeout');
}, 1);
}, 1);
28 changes: 28 additions & 0 deletions node/_tools/test/parallel/test-promise-unhandled-silent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 16.13.0
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually

// Flags: --unhandled-rejections=none
'use strict';

const common = require('../common');

// Verify that ignoring unhandled rejection works fine and that no warning is
// logged.

new Promise(() => {
throw new Error('One');
});

Promise.reject('test');

process.on('warning', common.mustNotCall('warning'));
process.on('uncaughtException', common.mustNotCall('uncaughtException'));
process.on('rejectionHandled', common.mustNotCall('rejectionHandled'));

process.on('unhandledRejection', common.mustCall(2));

setTimeout(common.mustCall(), 2);
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 16.13.0
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually

// Flags: --unhandled-rejections=throw
'use strict';

const common = require('../common');
const Countdown = require('../common/countdown');
const assert = require('assert');

// Verify that the unhandledRejection handler prevents triggering
// uncaught exceptions

const err1 = new Error('One');

const errors = [err1, null];

const ref = new Promise(() => {
throw err1;
});
// Explicitly reject `null`.
Promise.reject(null);

process.on('warning', common.mustNotCall('warning'));
process.on('rejectionHandled', common.mustNotCall('rejectionHandled'));
process.on('exit', assert.strictEqual.bind(null, 0));
process.on('uncaughtException', common.mustNotCall('uncaughtException'));

const timer = setTimeout(() => console.log(ref), 1000);

const counter = new Countdown(2, () => {
clearTimeout(timer);
});

process.on('unhandledRejection', common.mustCall((err) => {
counter.dec();
const knownError = errors.shift();
assert.deepStrictEqual(err, knownError);
}, 2));
24 changes: 24 additions & 0 deletions node/_tools/test/parallel/test-timers-interval-throw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 16.13.0
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually

'use strict';

const common = require('../common');
const assert = require('assert');

// To match browser behaviour, interval should continue
// being rescheduled even if it throws.

let count = 2;
const interval = setInterval(() => { throw new Error('IntervalError'); }, 1);

process.on('uncaughtException', common.mustCall((err) => {
assert.strictEqual(err.message, 'IntervalError');
if (--count === 0) {
clearInterval(interval);
}
}, 2));
46 changes: 46 additions & 0 deletions node/_tools/test/parallel/test-timers-uncaught-exception.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 16.13.0
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually

// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

'use strict';
const common = require('../common');
const assert = require('assert');
const errorMsg = 'BAM!';

// The first timer throws...
setTimeout(common.mustCall(function() {
throw new Error(errorMsg);
}), 1);

// ...but the second one should still run
setTimeout(common.mustCall(), 1);

function uncaughtException(err) {
assert.strictEqual(err.message, errorMsg);
}

process.on('uncaughtException', common.mustCall(uncaughtException));
26 changes: 26 additions & 0 deletions node/_tools/test/parallel/test-timers-unref-throw-then-ref.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 16.13.0
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually

'use strict';
const common = require('../common');
const assert = require('assert');

process.once('uncaughtException', common.mustCall((err) => {
common.expectsError({
message: 'Timeout Error'
})(err);
}));

let called = false;
const t = setTimeout(() => {
assert(!called);
called = true;
t.ref();
throw new Error('Timeout Error');
}, 1).unref();

setTimeout(common.mustCall(), 1);
Loading