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

Do not erase error if callback is called sync #18

Merged
merged 1 commit into from
Jul 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 8 additions & 10 deletions boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions test/after-and-ready.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
})