我需要一些帮助来尝试阻止节点抛出此错误,或者至少理解为什么我似乎无法捕获它:
events.js:72
throw er; // Unhandled 'error' event
^
Error: socket hang up
at SecurePair.error (tls.js:999:23)
at EncryptedStream.CryptoStream._done (tls.js:695:22)
at CleartextStream.read [as _read] (tls.js:496:24)
at CleartextStream.Readable.read (_stream_readable.js:320:10)
at EncryptedStream.onCryptoStreamFinish (tls.js:301:47)
at EncryptedStream.g (events.js:175:14)
at EncryptedStream.EventEmitter.emit (events.js:117:20)
at finishMaybe (_stream_writable.js:354:12)
at endWritable (_stream_writable.js:361:3)
at EncryptedStream.Writable.end (_stream_writable.js:339:5)
at EncryptedStream.CryptoStream.end (tls.js:633:31)
at Socket.onend (_stream_readable.js:483:10)
这是导致错误的代码段:
var req = https.request(options, function(res) {
res.setEncoding('utf8');
var buffer = '';
res.on('data', function(data) {
buffer += data;
});
res.on('end', function() {
try {
var json = JSON.parse(buffer);
} catch (err) {
return callback(err);
}
callback(null, json);
});
});
req.on('error', function(err) {
callback(err);
});
req.end(data);
api.prototype._get = function(action, callback, args) {
args = _.compactObject(args);
var path = '/api/' + action + '/?' + querystring.stringify(args);
this._request('get', path, undefined, callback, args)
}
api.prototype._post = function(action, callback, args) {
var path = '/api/' + action + '/';
args = _.compactObject(args);
var data = querystring.stringify(args);
this._request('post', path, data, callback, args);
}
为什么不req.on('错误'抓住这个错误?
节点版本:0.10.21
答案 0 :(得分:1)
您错过了响应的错误处理程序。
res.on('error', function errorHandler(err) { console.log(err); });