Skip to content

Commit

Permalink
event: throw on unhandled event
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts committed Jul 21, 2015
1 parent 4015299 commit 78f90d8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ function dispatcher () {
// handle cb's, and exit once done
// (str, fn) -> null
function retFn (action, cb) {
cb = cb || function () {}
if (action) return wait(action, endWrap)
endWrap()

function endWrap () {
if (cb) cb()
cb()
end()
}
}
Expand All @@ -93,8 +94,10 @@ function dispatcher () {
// emit an error
// any -> null
function emitErr (err) {
const emitter = actions.error ? actions.error[0] : function () {}
emitter(err)
if (!actions.error) {
throw new Error("unhandled 'error' event")
}
actions.error[0](err)
}
}
}
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,17 @@ test('.emit() should emit `error` on circular dependencies', function (t) {
d('bin')
})

test('.emit() should throw if no error handler is bound', function (t) {
t.plan(1)
const d = barracks()
d.on('bin', function (data, wait) {
return wait('bar')
})
d.on('bar', function (data, wait) {
return wait('bin')
})
t.throws(d.bind(null, 'bin'), /unhandled 'error' event/)
})

function noop () {}
noop()

0 comments on commit 78f90d8

Please sign in to comment.