Node.js 如何捕获getaddrinfo ENOTFOUND
我有一个链接列表,我需要在处理一些数据之前检查。使用http.get检查头返回错误:Node.js 如何捕获getaddrinfo ENOTFOUND,node.js,http,dns,getaddrinfo,Node.js,Http,Dns,Getaddrinfo,我有一个链接列表,我需要在处理一些数据之前检查。使用http.get检查头返回错误: events.js:72 throw er; // Unhandled 'error' event ^ Error: getaddrinfo ENOTFOUND at errnoException (dns.js:37:11) 我无法处理此错误,并退出该进程。我尝试了res.on(“error”)并尝试在http.get上捕获,
events.js:72
throw er; // Unhandled 'error' event
^
Error: getaddrinfo ENOTFOUND
at errnoException (dns.js:37:11)
我无法处理此错误,并退出该进程。我尝试了res.on(“error”)并尝试在http.get上捕获,但没有任何效果
下面是代码片段,以及
您只需要处理
错误
事件,如错误消息中所述。根据:
如果在请求过程中遇到任何错误(可能是DNS解析错误、TCP级别错误或实际HTTP解析错误),将在返回的请求对象上发出“错误”事件
下面是一个使用示例:
var getRequest = _http.get(options, function(res) {
// …
});
getRequest.on('error', function (err) {
console.log(err);
});
这将产生:
$ node test.js
{ [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }
在最高层,你可以
process.on('uncaughtException', function(err) {
console.log('### BIG ONE (%s)', err);
});
如果您使用
请求
.get('http://example.com/doodle.png')
.on('response',函数(response){
console.log(response.statusCode)//200
console.log(response.headers['content-type'])/'image/png'
})
.on('error',function(err){//Check out。可以告诉我解决方案吗?还需要补充一点,即同样的技术也适用于NPMrequest
模块。
process.on('uncaughtException', function(err) {
console.log('### BIG ONE (%s)', err);
});
request
.get('http://example.com/doodle.png')
.on('response', function(response) {
console.log(response.statusCode) // 200
console.log(response.headers['content-type']) // 'image/png'
})
.on('error', function(err) { // <------- add this
console.log(err)
});