जावास्क्रिप्ट कोड NodeJS में मार डाला निम्नलिखित पर विचार करें:NodeJS: http.ClientRequest विशिष्ट परिदृश्य में दो बार त्रुटि ईवेंट सक्रिय
// create ClientRequest
// port 55555 is not opened
var req = require('http').request('http://localhost:55555', function() {
console.log('should be never reached');
});
function cb() {
throw new Error();
}
req.on('error', function(e) {
console.log(e);
cb();
});
// exceptions handler
process.on('uncaughtException', function() {
console.log('exception caught. doing some async clean-up before exit...');
setTimeout(function() {
console.log('exiting');
process.exit(1);
}, 2000);
});
// send request
req.end();
अपेक्षित उत्पादन:
{ Error: connect ECONNREFUSED 127.0.0.1:55555
at Object.exports._errnoException (util.js:1026:11)
at exports._exceptionWithHostPort (util.js:1049:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1081:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 55555 }
exception caught. doing some async clean-up before exit...
exiting
वास्तविक उत्पादन:
{ Error: connect ECONNREFUSED 127.0.0.1:55555
at Object.exports._errnoException (util.js:1026:11)
at exports._exceptionWithHostPort (util.js:1049:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1081:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 55555 }
exception caught. doing some async clean-up before exit...
{ Error: socket hang up
at createHangUpError (_http_client.js:252:15)
at Socket.socketCloseListener (_http_client.js:284:23)
at emitOne (events.js:101:20)
at Socket.emit (events.js:188:7)
at TCP._handle.close [as _onclose] (net.js:492:12) code: 'ECONNRESET' }
exception caught. doing some async clean-up before exit...
exiting
जैसा कि आप देख सकते हैं, http.ClientRequest (या शायद stream.Writable?) त्रुटि घटना दो बार आग लगती है, पहले ECONNREFUSED और aft के साथ एर अपवाद पकड़ा गया है, ECONNRESET।
ऐसा नहीं होता है अगर हम httpTClientRequest त्रुटि हैंडलर में कॉलबैक को अतुल्यकालिक रूप से निष्पादित करते हैं तो अगलीटिक या सेटटाइमआउट का उपयोग करते हुए, उदा। यह परिवर्तन अपेक्षित व्यवहार देता है:
req.on('error', function(e) {
console.log(e);
process.nextTick(cb);
});
क्या कोई यह समझा सकता है कि यह क्यों हो रहा है और यदि यह एक बग या अपेक्षित काम करता है? व्यवहार नवीनतम नोड 4.x और नोड 6.x में समान है।
धन्यवाद!