Skip to content

Commit

Permalink
Fix issue with errors thrown in handlers
Browse files Browse the repository at this point in the history
When a callback has been provided to the handler, the error was
delivered in current tick -> bad!
  • Loading branch information
robertrossmann committed Feb 18, 2015
1 parent e0a9ad6 commit 5103f84
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/ledcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,17 @@ LEDController.register = function register (parser, handler) {
var that = this // scope
, emptyCallback = normaliseCallback() // This will be fun!
, data
done = normaliseCallback(done)

try {
// Summon the handler to do the master's bidding
data = handler(input)
} catch (e) {
return done(e)
// If we have a callback, we should always call it in next tick
if (typeof done === 'function')
return setImmediate(done, e)

// No callback -> no mercy!
throw e
}

// Normalise
Expand Down
7 changes: 6 additions & 1 deletion test/ledcontroller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,16 +290,21 @@ describe('LEDController', function () {
})

it('should pass all errors thrown from handlers to the method callback', function (done) {
// Simple asynchrony test - see assertion below
var ticked = false

LEDController.register('dummymethod', function () {
// Imagine an error in my custom serialiser happened...
throw new Error('test')
})

ledGreen.dummymethod('input', function (err) {
err.message.should.equal('test')

ticked.should.equal(true, 'In callbacks, handler errors should be delivered in next tick')
done()
})

ticked = true
})

it('should throw if handler throws and no callback has been provided', function () {
Expand Down

0 comments on commit 5103f84

Please sign in to comment.