Skip to content

How to handle error when host does not respond? #76

@tomsoderlund

Description

@tomsoderlund

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions