mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
fix: #8120, bubble errors from static hooks
This commit is contained in:
@@ -148,8 +148,13 @@ module.exports = function (Plugins) {
|
|||||||
if (!Array.isArray(hookList) || !hookList.length) {
|
if (!Array.isArray(hookList) || !hookList.length) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// don't bubble errors from these hooks, so bad plugins don't stop startup
|
||||||
|
const noErrorHooks = ['static:app.load', 'static:assets.prepare', 'static:app.preload'];
|
||||||
await async.each(hookList, function (hookObj, next) {
|
await async.each(hookList, function (hookObj, next) {
|
||||||
if (typeof hookObj.method === 'function') {
|
if (typeof hookObj.method !== 'function') {
|
||||||
|
return next();
|
||||||
|
}
|
||||||
|
|
||||||
let timedOut = false;
|
let timedOut = false;
|
||||||
const timeoutId = setTimeout(function () {
|
const timeoutId = setTimeout(function () {
|
||||||
winston.warn('[plugins] Callback timed out, hook \'' + hook + '\' in plugin \'' + hookObj.id + '\'');
|
winston.warn('[plugins] Callback timed out, hook \'' + hook + '\' in plugin \'' + hookObj.id + '\'');
|
||||||
@@ -157,16 +162,14 @@ module.exports = function (Plugins) {
|
|||||||
next();
|
next();
|
||||||
}, 5000);
|
}, 5000);
|
||||||
|
|
||||||
const onError = (err) => {
|
const callback = (err) => {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
if (err) {
|
||||||
winston.error('[plugins] Error executing \'' + hook + '\' in plugin \'' + hookObj.id + '\'');
|
winston.error('[plugins] Error executing \'' + hook + '\' in plugin \'' + hookObj.id + '\'');
|
||||||
winston.error(err);
|
winston.error(err.stack);
|
||||||
clearTimeout(timeoutId);
|
}
|
||||||
next();
|
|
||||||
};
|
|
||||||
const callback = (...args) => {
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
if (!timedOut) {
|
if (!timedOut) {
|
||||||
next(...args);
|
next(noErrorHooks.includes(hook) ? null : err);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
@@ -174,14 +177,11 @@ module.exports = function (Plugins) {
|
|||||||
if (utils.isPromise(returned)) {
|
if (utils.isPromise(returned)) {
|
||||||
returned.then(
|
returned.then(
|
||||||
payload => setImmediate(callback, null, payload),
|
payload => setImmediate(callback, null, payload),
|
||||||
err => setImmediate(onError, err)
|
err => setImmediate(callback, err)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
onError(err);
|
callback(err);
|
||||||
}
|
|
||||||
} else {
|
|
||||||
next();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ describe('Plugins', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should register and fire a static hook returning a promise that gets rejected with a warning only', function (done) {
|
it('should register and fire a static hook returning a promise that gets rejected with a error', function (done) {
|
||||||
function method(data) {
|
function method(data) {
|
||||||
assert.equal(data.bar, 'test');
|
assert.equal(data.bar, 'test');
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
@@ -129,7 +129,8 @@ describe('Plugins', function () {
|
|||||||
}
|
}
|
||||||
plugins.registerHook('test-plugin', { hook: 'static:test.hook', method: method });
|
plugins.registerHook('test-plugin', { hook: 'static:test.hook', method: method });
|
||||||
plugins.fireHook('static:test.hook', { bar: 'test' }, function (err) {
|
plugins.fireHook('static:test.hook', { bar: 'test' }, function (err) {
|
||||||
assert.ifError(err);
|
assert.strictEqual(err.message, 'just because');
|
||||||
|
plugins.unregisterHook('test-plugin', 'static:test.hook', method);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -144,6 +145,7 @@ describe('Plugins', function () {
|
|||||||
plugins.registerHook('test-plugin', { hook: 'static:test.hook', method: method });
|
plugins.registerHook('test-plugin', { hook: 'static:test.hook', method: method });
|
||||||
plugins.fireHook('static:test.hook', { bar: 'test' }, function (err) {
|
plugins.fireHook('static:test.hook', { bar: 'test' }, function (err) {
|
||||||
assert.ifError(err);
|
assert.ifError(err);
|
||||||
|
plugins.unregisterHook('test-plugin', 'static:test.hook', method);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user