@@ -26,14 +26,15 @@ var common = require('../common');
2626var assert = require ( 'assert' ) ;
2727var domain = require ( 'domain' ) ;
2828var events = require ( 'events' ) ;
29+ var fs = require ( 'fs' ) ;
2930var caught = 0 ;
30- var expectCaught = 8 ;
31+ var expectCaught = 0 ;
3132
3233var d = new domain . Domain ( ) ;
3334var e = new events . EventEmitter ( ) ;
3435
3536d . on ( 'error' , function ( er ) {
36- console . error ( 'caught' , er ) ;
37+ console . error ( 'caught' , er && ( er . message || er ) ) ;
3738
3839 var er_message = er . message ;
3940 var er_path = er . path
@@ -110,24 +111,58 @@ d.on('error', function(er) {
110111 break ;
111112
112113 default :
113- console . error ( 'unexpected error, throwing %j' , er . message ) ;
114+ console . error ( 'unexpected error, throwing %j' , er . message || er ) ;
114115 throw er ;
115116 }
116117
117118 caught ++ ;
118119} ) ;
119120
121+
122+
120123process . on ( 'exit' , function ( ) {
121- console . error ( 'exit' ) ;
122- assert . equal ( caught , expectCaught ) ;
124+ console . error ( 'exit' , caught , expectCaught ) ;
125+ assert . equal ( caught , expectCaught , 'caught the expected number of errors' ) ;
123126 console . log ( 'ok' ) ;
124127} ) ;
125128
126129
130+ // catch thrown errors no matter how many times we enter the event loop
131+ // this only uses implicit binding, except for the first function
132+ // passed to d.run(). The rest are implicitly bound by virtue of being
133+ // set up while in the scope of the d domain.
134+ d . run ( function ( ) {
135+ process . nextTick ( function ( ) {
136+ var i = setInterval ( function ( ) {
137+ clearInterval ( i ) ;
138+ setTimeout ( function ( ) {
139+ fs . stat ( 'this file does not exist' , function ( er , stat ) {
140+ // uh oh! stat isn't set!
141+ // pretty common error.
142+ console . log ( stat . isDirectory ( ) ) ;
143+ } ) ;
144+ } ) ;
145+ } ) ;
146+ } ) ;
147+ } ) ;
148+ expectCaught ++ ;
149+
150+
151+
152+ // implicit addition of a timer created within a domain-bound context.
153+ d . run ( function ( ) {
154+ setTimeout ( function ( ) {
155+ throw new Error ( 'implicit timer' ) ;
156+ } ) ;
157+ } ) ;
158+ expectCaught ++ ;
159+
160+
127161
128162// Event emitters added to the domain have their errors routed.
129163d . add ( e ) ;
130164e . emit ( 'error' , new Error ( 'emitted' ) ) ;
165+ expectCaught ++ ;
131166
132167
133168
@@ -141,6 +176,9 @@ function fn(er) {
141176
142177var bound = d . intercept ( fn ) ;
143178bound ( new Error ( 'bound' ) ) ;
179+ expectCaught ++ ;
180+
181+
144182
145183// intercepted should never pass first argument to callback
146184function fn2 ( data ) {
@@ -169,36 +207,16 @@ function thrower() {
169207 throw new Error ( 'thrown' ) ;
170208}
171209setTimeout ( d . bind ( thrower ) , 100 ) ;
210+ expectCaught ++ ;
172211
173212
174213
175214// Pass an intercepted function to an fs operation that fails.
176- var fs = require ( 'fs' ) ;
177215fs . open ( 'this file does not exist' , 'r' , d . intercept ( function ( er ) {
178216 console . error ( 'should not get here!' , er ) ;
179217 throw new Error ( 'should not get here!' ) ;
180218} , true ) ) ;
181-
182-
183-
184- // catch thrown errors no matter how many times we enter the event loop
185- // this only uses implicit binding, except for the first function
186- // passed to d.run(). The rest are implicitly bound by virtue of being
187- // set up while in the scope of the d domain.
188- d . run ( function ( ) {
189- process . nextTick ( function ( ) {
190- var i = setInterval ( function ( ) {
191- clearInterval ( i ) ;
192- setTimeout ( function ( ) {
193- fs . stat ( 'this file does not exist' , function ( er , stat ) {
194- // uh oh! stat isn't set!
195- // pretty common error.
196- console . log ( stat . isDirectory ( ) ) ;
197- } ) ;
198- } ) ;
199- } ) ;
200- } ) ;
201- } ) ;
219+ expectCaught ++ ;
202220
203221
204222
@@ -213,16 +231,9 @@ setTimeout(function() {
213231 // escape from the domain, but implicit is still bound to it.
214232 implicit . emit ( 'error' , new Error ( 'implicit' ) ) ;
215233} , 10 ) ;
234+ expectCaught ++ ;
216235
217236
218-
219- // implicit addition of a timer created within a domain-bound context.
220- d . run ( function ( ) {
221- setTimeout ( function ( ) {
222- throw new Error ( 'implicit timer' ) ;
223- } ) ;
224- } ) ;
225-
226237var result = d . run ( function ( ) {
227238 return 'return value' ;
228239} ) ;
@@ -231,3 +242,4 @@ assert.equal(result, 'return value');
231242
232243var fst = fs . createReadStream ( 'stream for nonexistent file' )
233244d . add ( fst )
245+ expectCaught ++ ;
0 commit comments