Skip to content
Browse files

Rethrow if error inside end, error or both event

  • Loading branch information...
1 parent 8684953 commit 8cb7b6767974b69dbd373e4bd88c430d30d9a375 @wdavidw committed Mar 27, 2013
Showing with 29 additions and 5 deletions.
  1. +4 −2 lib/each.js
  2. +2 −2 src/each.coffee
  3. +23 −1 test/error.coffee
View
6 lib/each.js
@@ -222,9 +222,11 @@ module.exports = function(elements) {
next(err);
}
}
- } catch (e) {
+ } catch (err) {
if (eacher.readable) {
- next(e);
+ next(err);
+ } else {
+ throw err;
}
}
}
View
4 src/each.coffee
@@ -150,10 +150,10 @@ module.exports = (elements) ->
args.push next unless sync
err = emit args...
next err if sync
- catch e
+ catch err
# prevent next to be called if an error occurend inside the
# error, end or both callbacks
- next e if eacher.readable
+ if eacher.readable then next err else throw err
null
next = (err) ->
errors.push err if err? and err instanceof Error
View
24 test/error.coffee
@@ -32,7 +32,7 @@ describe 'Error', ->
error_called.should.be.ok
error_assert err
next()
- it 'Concurrent # throw error', (next) ->
+ it 'Concurrent handle thrown error', (next) ->
current = 0
error_called = false
error_assert = (err) ->
@@ -189,4 +189,26 @@ describe 'Error', ->
next()
.on 'end', (err) ->
false.should.be.ok
+ it 'rethrow if error inside end', (next) ->
+ lsts = process.listeners 'uncaughtException'
+ process.removeAllListeners 'uncaughtException'
+ process.on 'uncaughtException', (err) ->
+ err.message.should.eql 'nonexistentFunc is not defined'
+ process.removeAllListeners 'uncaughtException'
+ for lst in lsts
+ process.on 'uncaughtException', lst
+ next()
+ count = 0
+ eacher = each()
+ .parallel(null)
+ .times(10)
+ .on 'item', (element, index, next) ->
+ count++
+ return next() if index < 5
+ return eacher.end() if index is 5
+ false.should.be.ok
+ .on 'error', (err) ->
+ next err
+ .on 'end', ->
+ nonexistentFunc()

0 comments on commit 8cb7b67

Please sign in to comment.
Something went wrong with that request. Please try again.