Skip to content

Commit

Permalink
Add robustness against dns errors and better logging when no redirect…
Browse files Browse the repository at this point in the history
…s are present
  • Loading branch information
Munter committed Jul 21, 2014
1 parent 6c7d2b7 commit 0590802
Showing 1 changed file with 40 additions and 17 deletions.
57 changes: 40 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,35 @@ module.exports = function (options) {
}

function colorStatus(status) {
if (status < 300) {
return chalk.green(status);
} else if (status < 400) {
return chalk.yellow(status);
if (typeof status === 'number') {
if (status < 300) {
return chalk.green(status);

This comment has been minimized.

This comment has been minimized.

Copy link
@Munter

Munter Jul 24, 2014

Author Owner

I'm not done messing with the logging output here, but we should certainly use it in https://github.com/assetgraph/assetgraph/blob/master/lib/transforms/logEvents.js#L32-L33

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Jul 24, 2014

On a related note 0.002 is weird. I would much prefer it in milliseconds when that low.

See: https://github.com/sindresorhus/pretty-ms

You could probably use https://github.com/sindresorhus/pretty-bytes for some prettier size logging too.

} else if (status < 400) {
return chalk.yellow(status);
} else {
return chalk.red(status);
}
} else {
return chalk.red(status);
}
}

function logHttpResult(status, url, redirects, relations) {
redirects = redirects || [];
relations = relations || [];

if (status !== 200 || redirects.length || options.verbose) {
redirects.forEach(function (redirect) {
process.stdout.write(colorStatus(redirect.statusCode) + ' ' + redirect.redirectUri + chalk.yellow(' --> '));
});

console.log(colorStatus(status), url);

if (redirects.length) {
_.uniq(relations.map(function (relation) {
return relation.from.urlOrDescription.replace(/#.*$/, '');
})).forEach(function (url) {
console.log('\t' + chalk.cyan(url));
});
}
_.uniq(relations.map(function (relation) {
return relation.from.urlOrDescription.replace(/#.*$/, '');
})).forEach(function (url) {
console.log('\t' + chalk.cyan(url));
});
}
}

Expand All @@ -58,16 +63,34 @@ module.exports = function (options) {
strictSSL: true,
gzip: true
}, function (error, res) {
if (error) {
console.error('httpError', url, error);
}
var status,
redirects;

var status = res.statusCode,
if (error) {
var code = error.code;

if (code) {
if (code === 'ENOTFOUND') {
status = 'DNS Missing';
} else {
status = code;
}
} else {
status = 'Unknown error';
}

logHttpResult(status, url, undefined, relations);

callback(undefined, status);
} else {
status = res.statusCode;
redirects = res.request.redirects;

logHttpResult(status, url, redirects, relations);
logHttpResult(status, url, redirects, relations);

callback(undefined, redirects[0] && redirects[0].statusCode || status);
}

callback(undefined, redirects[0] && redirects[0].statusCode || status);
});
};
}
Expand Down

0 comments on commit 0590802

Please sign in to comment.