Permalink
Browse files

Improve render errors

  • Loading branch information...
Kikobeats committed May 8, 2018
1 parent 7dd79f5 commit 53f91375c0c1217be18cad2800ed87f5e6669f23
Showing with 58 additions and 27 deletions.
  1. +11 −26 bin/cli/index.js
  2. +45 −0 bin/cli/render-error.js
  3. +2 −1 package.json
@@ -2,17 +2,15 @@
'use strict'
const { chain, size, concat, first, isEmpty } = require('lodash')
const { size, concat, first, isEmpty } = require('lodash')
const AggregateError = require('aggregate-error')
const normalizeUrl = require('normalize-url')
const reachableUrl = require('reachable-url')
const { STATUS_CODES } = require('http')
const dnsErrors = require('dnserrors')
const urlint = require('urlint')
const isCI = require('is-ci')
const { gray, red } = require('../view/colorize')
const extractUrls = require('./extract-urls')
const renderError = require('./render-error')
const pkg = require('../../package.json')
const view = require('../view')
@@ -22,24 +20,6 @@ const getUrl = async input => {
return url
}
const messageError = ({ errors, url: originalUrl }) => {
return chain(errors)
.first()
.thru(err => {
err = Object.assign({}, err, dnsErrors(err))
const url = err.url || originalUrl
const statusCode = err.statusCode || err.status
const httpMessage = STATUS_CODES[statusCode] || 'Error'
return { ...err, httpMessage, statusCode, url }
})
.thru(({ httpMessage, statusCode, method, url }) => {
const status = statusCode ? `(${red(statusCode)}) ` : ''
const message = status ? gray(httpMessage) : red(httpMessage)
return `${gray(`${message} ${status}${normalizeUrl(url)}`)}`
})
.value()
}
require('update-notifier')({ pkg }).notify()
const cli = require('meow')(require('./help'), {
@@ -112,10 +92,15 @@ let url
const emitter = await urlint(urls, opts)
view({ total: size(urls), emitter, ...opts })
} catch (error) {
const errors = error instanceof AggregateError ? Array.from(error) : [error]
const message = messageError({ errors, url })
console.log(message)
} catch (genericError) {
const errors =
genericError instanceof AggregateError
? Array.from(genericError)
: [genericError]
const error = first(errors)
const prettyError = renderError(error)
console.log(prettyError)
process.exit(1)
}
})()
@@ -0,0 +1,45 @@
'use strict'
const PrettyError = require('pretty-error')
const cleanStack = require('clean-stack')
const pe = new PrettyError()
pe.appendStyle({
// this is a simple selector to the element that says 'Error'
'pretty-error > header > title > kind': {
background: 'none',
color: 'bright-red',
marginRight: 1
},
'pretty-error > header > colon': {
display: 'none'
},
'pretty-error > header > message': {
color: 'grey'
},
'pretty-error > trace > item > header > pointer > file': {
color: 'grey'
},
'pretty-error > trace > item > header > pointer > colon': {
color: 'grey'
},
'pretty-error > trace > item > header > pointer > line': {
color: 'grey'
},
'pretty-error > trace > item > header > what': {
color: 'grey'
}
})
module.exports = error => {
const stack = cleanStack(error.stack)
const cleanError = Object.assign({}, error, { stack })
return pe.render(cleanError)
}
@@ -36,7 +36,7 @@
"aggregate-error": "~1.0.0",
"chalk": "~2.4.1",
"cheerio": "~1.0.0-rc.2",
"dnserrors": "~2.0.3",
"clean-stack": "~1.3.0",
"got": "~8.3.1",
"html-urls": "~1.0.5",
"is-ci": "~1.1.0",
@@ -45,6 +45,7 @@
"neat-log": "~2.3.0",
"normalize-url": "~2.0.1",
"ora": "~2.1.0",
"pretty-error": "~2.1.1",
"pretty-ms": "~3.1.0",
"reachable-url": "~1.0.1",
"update-notifier": "~2.5.0",

0 comments on commit 53f9137

Please sign in to comment.