Permalink
Browse files

Improve error feedback

  • Loading branch information...
Kikobeats committed May 2, 2018
1 parent 3975a7a commit f032e002f2fba37671a37a5afea87a42a8aed68e
Showing with 28 additions and 10 deletions.
  1. +26 −10 bin/cli/index.js
  2. +2 −0 package.json
@@ -2,23 +2,41 @@
'use strict'
const { size, concat, first, isEmpty } = require('lodash')
const { chain, size, concat, first, isEmpty } = require('lodash')
const normalizeUrl = require('normalize-url')
const reachableUrl = require('reachable-url')
const dnsErrors = require('dnserrors')
const { STATUS_CODES } = require('http')
const urlint = require('urlint')
const isCI = require('is-ci')
const got = require('got')
const { gray, red } = require('../view/colorize')
const extractUrls = require('./extract-urls')
const colorize = require('../view/colorize')
const pkg = require('../../package.json')
const view = require('../view')
const getUrl = async input => {
const normalizedUrl = normalizeUrl(input)
const { url } = await got.head(normalizedUrl)
const { url } = await reachableUrl(normalizedUrl)
return url
}
const messageError = errors => {
return chain(errors)
.first()
.thru(err => {
err = dnsErrors(err)
const statusCode = err.statusCode || err.status
const httpMessage = STATUS_CODES[statusCode]
return { ...err, httpMessage, statusCode }
})
.thru(
({ httpMessage, statusCode, method, url }) =>
`${gray(`${httpMessage} (${red(statusCode)}) ${normalizeUrl(url)}`)}`
)
.value()
}
require('update-notifier')({ pkg }).notify()
const cli = require('meow')(require('./help'), {
@@ -80,12 +98,10 @@ if (isEmpty(cli.input)) {
const emitter = await urlint(urls, opts)
view({ total: size(urls), emitter, ...opts })
} catch (err) {
let message
if (err.name && err.message) message = `${err.name}: ${err.message}`
else if (err.message) message = err.message
else message = err
console.log(colorize.red(message))
} catch (aggregatedError) {
const error = chain(Array.from(aggregatedError))
const message = messageError(error)
console.log(message)
process.exit(1)
}
})()
@@ -34,6 +34,7 @@
"dependencies": {
"@metascraper/helpers": "~3.10.7",
"chalk": "~2.4.1",
"dnserrors": "~2.0.3",
"got": "~8.3.0",
"html-urls": "~1.0.5",
"is-ci": "~1.1.0",
@@ -43,6 +44,7 @@
"normalize-url": "~2.0.1",
"ora": "~2.1.0",
"pretty-ms": "~3.1.0",
"reachable-url": "~1.0.0",
"update-notifier": "~2.5.0",
"urlint": "~1.1.0",
"xml-urls": "~1.0.1"

0 comments on commit f032e00

Please sign in to comment.