diff --git a/boot.js b/boot.js index 23f1344..fb4dec2 100644 --- a/boot.js +++ b/boot.js @@ -174,8 +174,6 @@ Boot.prototype._addPlugin = function (plugin, opts, callback) { } Boot.prototype.after = function (func, cb) { - // TODO do not rely on .use() - // const server = this._server this.use(function (s, opts, done) { callWithCbOrNextTick.call(this, func, done) }.bind(this), cb) @@ -203,20 +201,20 @@ Boot.prototype.ready = function (func) { function noop () {} function callWithCbOrNextTick (func, cb, context) { - if (this && this._server) { - context = this._server - } + context = this._server + var err = this._error + + // with this the error will appear just in the next after/ready callback + this._error = null if (func.length === 0 || func.length === 1) { - func(this._error) + func(err) process.nextTick(cb) } else if (func.length === 2) { - func(this._error, cb) + func(err, cb) } else { - func(this._error, context, cb) + func(err, context, cb) } - // with this the error will appear just in the next after/ready callback - this._error = null } module.exports = Boot diff --git a/test/after-and-ready.js b/test/after-and-ready.js index 214053b..9f70f16 100644 --- a/test/after-and-ready.js +++ b/test/after-and-ready.js @@ -342,3 +342,33 @@ test('if `use` has a callback without parameters, the error must reach ready', ( t.ok(err) }) }) + +test('shold pass the errors from after to ready', (t) => { + t.plan(6) + + const server = {} + const app = boot(server, {}) + + server.use(function (s, opts, done) { + t.equal(s, server, 'the first argument is the server') + t.deepEqual(opts, {}, 'no options') + done() + }).after((err, done) => { + t.error(err) + done(new Error('some error')) + }) + + server.onClose(() => { + t.ok('onClose called') + }) + + server.ready(err => { + t.is(err.message, 'some error') + }) + + app.on('start', () => { + server.close(() => { + t.pass('booted') + }) + }) +})