node/test/parallel/test-async-wrap-throw-from-callback.js
Trevor Norris a17200b520 async_wrap: don't abort on callback exception
Rather than abort if the init/pre/post/final/destroy callbacks throw,
force the exception to propagate and not be made catchable. This way
the application is still not allowed to proceed but also allowed the
location of the failure to print before exiting. Though the stack itself
may not be of much use since all callbacks except init are called from
the bottom of the call stack.

    /tmp/async-test.js:14
      throw new Error('pre');
      ^
    Error: pre
        at InternalFieldObject.pre (/tmp/async-test.js:14:9)

PR-URL: https://github.com/nodejs/node/pull/5756
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
2016-03-28 11:32:54 -06:00

69 lines
1.6 KiB
JavaScript

'use strict';
require('../common');
const async_wrap = process.binding('async_wrap');
const assert = require('assert');
const crypto = require('crypto');
const domain = require('domain');
const spawn = require('child_process').spawn;
const callbacks = [ 'init', 'pre', 'post', 'destroy' ];
const toCall = process.argv[2];
var msgCalled = 0;
var msgReceived = 0;
function init() {
if (toCall === 'init')
throw new Error('init');
}
function pre() {
if (toCall === 'pre')
throw new Error('pre');
}
function post() {
if (toCall === 'post')
throw new Error('post');
}
function destroy() {
if (toCall === 'destroy')
throw new Error('destroy');
}
if (typeof process.argv[2] === 'string') {
async_wrap.setupHooks({ init, pre, post, destroy });
async_wrap.enable();
process.on('uncaughtException', () => assert.ok(0, 'UNREACHABLE'));
const d = domain.create();
d.on('error', () => assert.ok(0, 'UNREACHABLE'));
d.run(() => {
// Using randomBytes because timers are not yet supported.
crypto.randomBytes(0, () => { });
});
} else {
process.on('exit', (code) => {
assert.equal(msgCalled, callbacks.length);
assert.equal(msgCalled, msgReceived);
});
callbacks.forEach((item) => {
msgCalled++;
const child = spawn(process.execPath, [__filename, item]);
var errstring = '';
child.stderr.on('data', (data) => {
errstring += data.toString();
});
child.on('close', (code) => {
if (errstring.includes('Error: ' + item))
msgReceived++;
assert.equal(code, 1, `${item} closed with code ${code}`);
});
});
}