/
error.coffee
192 lines (189 loc) · 6.16 KB
/
error.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
should = require 'should'
each = if process.env.EACH_COV then require '../lib-cov/each' else require '../lib/each'
describe 'Error', ->
it 'Concurrent # error and both callbacks', (next) ->
current = 0
error_called = false
error_assert = (err) ->
current.should.eql 9
err.message.should.eql 'Multiple errors (2)'
err.errors.length.should.eql 2
err.errors[0].message.should.eql 'Testing error in 6'
err.errors[1].message.should.eql 'Testing error in 7'
each( [ {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}, {id: 7}, {id: 8}, {id: 9}, {id: 10}, {id: 11} ] )
.parallel( 4 )
.on 'item', (element, index, next) ->
index.should.eql current
current++
setTimeout ->
if element.id is 6 or element.id is 7
next new Error "Testing error in #{element.id}"
else
next()
, 100
.on 'error', (err) ->
error_assert err
error_called = true
.on 'end', (err) ->
assert.ok false
.on 'both', (err) ->
error_called.should.be.ok
error_assert err
next()
it 'Concurrent # throw error', (next) ->
current = 0
error_called = false
error_assert = (err) ->
current.should.eql 6
err.message.should.eql 'Testing error in 6'
err.errors.length.should.eql 0
each( [ {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}, {id: 6}, {id: 7}, {id: 8}, {id: 9}, {id: 10}, {id: 11} ] )
.parallel( 4 )
.on 'item', (element, index, next) ->
index.should.eql current
current++
if element.id is 6 or element.id is 7
throw new Error "Testing error in #{element.id}"
else
next()
.on 'error', (err) ->
error_assert err
error_called = true
.on 'end', (err) ->
assert.ok false
.on 'both', (err) ->
error_called.should.be.ok
error_assert err
next()
it 'Parallel # multiple errors # error callback', (next) ->
# when multiple errors are thrown, a new error object is created
# with a message indicating the number of errors. The original
# errors are available as an array in the second argument of the
# `error` event.
current = 0
each( [{id: 1}, {id: 2}, {id: 3}, {id: 4}] )
.parallel( true )
.on 'item', (element, index, next) ->
index.should.eql current
current++
setTimeout ->
if element.id is 1 or element.id is 3
next( new Error "Testing error in #{element.id}" )
else
next()
, 100
.on 'error', (err) ->
err.message.should.eql 'Multiple errors (2)'
err.errors.length.should.eql 2
err.errors[0].message.should.eql 'Testing error in 1'
err.errors[1].message.should.eql 'Testing error in 3'
return next()
.on 'end', (err) ->
false.should.be.ok
it 'Parallel # single error # error callback', (next) ->
# when on one error is thrown, the error is passed to
# the `error` event as is as well as a single element array
# of the second argument.
current = 0
each( [{id: 1}, {id: 2}, {id: 3}, {id: 4}] )
.parallel( true )
.on 'item', (element, index, next) ->
index.should.eql current
current++
setTimeout ->
if element.id is 3
next( new Error "Testing error in #{element.id}" )
else
next()
, 100
.on 'error', (err) ->
err.message.should.eql 'Testing error in 3'
return next()
.on 'end', (err) ->
false.should.be.ok
it 'Parallel # async # both callback', (next) ->
current = 0
each( [{id: 1}, {id: 2}, {id: 3}, {id: 4}] )
.parallel( true )
.on 'item', (element, index, next) ->
index.should.eql current
current++
setTimeout ->
if element.id is 1 or element.id is 3
next( new Error "Testing error in #{element.id}" )
else
next()
, 100
.on 'both', (err) ->
err.message.should.eql 'Multiple errors (2)'
err.errors.length.should.eql 2
err.errors[0].message.should.eql 'Testing error in 1',
err.errors[1].message.should.eql 'Testing error in 3'
return next()
.on 'end', (err) ->
false.should.be.ok
it 'Parallel # sync # both callback', (next) ->
current = 0
each( [{id: 1}, {id: 2}, {id: 3}, {id: 4}] )
.parallel( true )
.on 'item', (element, index, next) ->
index.should.eql current
current++
if element.id is 1 or element.id is 3
next( new Error "Testing error in #{element.id}" )
else setTimeout next, 100
.on 'both', (err) ->
# In this specific case, since the item handler
# send error sequentially, we are only receiving
# one error
err.message.should.eql 'Testing error in 1'
next()
it 'Sequential # sync # error callback', (next) ->
current = 0
each( [ {id: 1}, {id: 2}, {id: 3} ] )
.on 'item', (element, index, next) ->
index.should.eql current
current++
if element.id is 2
next( new Error 'Testing error' )
else next()
.on 'error', (err) ->
err.message.should.eql 'Testing error'
next()
.on 'end', (err) ->
false.should.be.ok
it 'Sequential # async # error callback', (next) ->
current = 0
each( [ {id: 1}, {id: 2}, {id: 3} ] )
.on 'item', (element, index, next) ->
index.should.eql current
current++
if element.id is 2
setTimeout ->
next( new Error 'Testing error' )
, 100
else setTimeout next, 100
.on 'error', (err) ->
err.message.should.eql 'Testing error'
next()
.on 'end', (err) ->
false.should.be.ok
it 'catch undefined', (next) ->
each( [ 1, 2, 3 ] )
.on 'item', (element, index, next) ->
Toto.should.throw.an.error
.on 'error', (err) ->
err.message.should.eql 'Toto is not defined'
next()
.on 'end', (err) ->
false.should.be.ok
it 'catch TypeError in concurrent mode', (next) ->
each( [ 1, 2, 3 ] )
.parallel( 4 )
.on 'item', (element, index, next) ->
undefined.should.throw.an.error
.on 'error', (err) ->
err.message.should.eql 'Cannot read property \'should\' of undefined'
next()
.on 'end', (err) ->
false.should.be.ok