How to handle error when host does not respond?
tomsoderlund opened this issue · 1 comments
I'm using html-metadata
to parse a list of hostnames:
const htmlMetadata = require('html-metadata');
console.log(`Domain: ${domain}`);
const newUrl = completeUrl(domain);
htmlMetadata(newUrl, (err, data) => {
console.log(`htmlMetadata callback:`, err);
const newData = {
url: newUrl,
shortName: _.capitalize(newUrl.split('.')[1]),
tld: newUrl.split('.').pop(),
title: _.get(data, 'general.title', '').replace(ALL_LINEBREAKS, '').replace(ALL_TABS, ''),
description: _.get(data, 'general.description', '').replace(ALL_LINEBREAKS, '').replace(ALL_TABS, ''),
}
cb(null, newData); // even if err, don't propagate it
});
However, when host does not respond, I get this error:
Domain: f-edition.se
_http_agent.js:186
nextTick(newSocket._handle.getAsyncId(), function() {
^
TypeError: Cannot read property '_handle' of undefined
at _http_agent.js:186:27
at Timeout._onTimeout (/Users/tomsoderlund/Documents/Projects/Weld.io/Development/weld-extensions/cabal/node_modules/preq/index.js:24:17)
at ontimeout (timers.js:488:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:283:5)
Please note that this happens before my htmlMetadata
callback is triggered.
I've tried wrapping my code in try/catch
but it doesn't help, I guess it's because it's async and the error is happening in a different thread.
Any tips?
Hmm, unfortunately I haven't used the callback version of these methods at all; they're all bluebird Promises and wrapped using Promise.nodeify() and we just use the native Promise version. See: http://bluebirdjs.com/docs/api/ascallback.html - maybe that documentation about how to handle errors will be helpful? I do know that a lot of times "promisify" or "nodeify" methods don't always work as expected.