-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add robustness against dns errors and better logging when no redirect…
…s are present
- Loading branch information
Showing
1 changed file
with
40 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Munter
Author
Owner
|
||
} 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)); | ||
}); | ||
} | ||
} | ||
|
||
|
@@ -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); | ||
}); | ||
}; | ||
} | ||
|
You might want https://github.com/sindresorhus/log-symbols