Skip to content

Commit

Permalink
Improve printing
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Oct 16, 2018
1 parent 30ce71f commit 5530e66
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 33 deletions.
12 changes: 9 additions & 3 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ const rejectionHandled = function(context, promise) {
handleEvent({ ...context, promise })
}

// eslint-disable-next-line no-inline-comments
const multipleResolves = function(context, type, promise /*, promiseValue */) {
handleEvent({ ...context, promise })
// eslint-disable-next-line max-params
const multipleResolves = function(context, type, promise, secondPromiseValue) {
const secondPromiseState = TYPE_TO_STATE[type]
handleEvent({ ...context, promise, secondPromiseState, secondPromiseValue })
}

const TYPE_TO_STATE = {
resolve: 'resolved',
reject: 'rejected',
}

module.exports = {
Expand Down
4 changes: 4 additions & 0 deletions src/handle.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const handleEvent = async function({
error,
promise,
promiseValue,
secondPromiseState,
secondPromiseValue,
}) {
const { promiseState, promiseValue: promiseValueA } = await parsePromise({
eventName,
Expand All @@ -22,6 +24,8 @@ const handleEvent = async function({
eventName,
promiseState,
promiseValue: promiseValueA,
secondPromiseState,
secondPromiseValue,
error,
})

Expand Down
109 changes: 79 additions & 30 deletions src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,27 @@

const { platform } = require('process')

const { red, yellow, bold, dim } = require('chalk')
const { red, yellow, bold, dim, inverse } = require('chalk')

// Retrieve `message` which sums up all information that can be gathered about
// the event.
const getMessage = function({ eventName, promiseState, promiseValue, error }) {
const getMessage = function({
eventName,
promiseState,
promiseValue,
secondPromiseState,
secondPromiseValue,
error,
}) {
const header = getHeader({ eventName, error })

const content = getContent({ promiseState, promiseValue, error })
const content = getContent({
promiseState,
promiseValue,
secondPromiseState,
secondPromiseValue,
error,
})

const message = `${header}\n${dim(content)}`
return message
Expand All @@ -24,60 +37,96 @@ const getHeader = function({ eventName, error }) {
}

// `warning` events use `Error.name|code|detail` in `message`
const getWarningHeader = function({ error: { code, detail } }) {
const codeMessage = code === undefined ? '' : ` (${code})`
const detailMessage = detail === undefined ? '' : `: ${detail}`

return `${codeMessage}${detailMessage}`
const getWarningHeader = function({ error: { code, detail = '' } }) {
const codeMessage = code === undefined ? '' : `(${code}) `
return `${codeMessage}${detail}`
}

const HEADERS = {
uncaughtException: 'uncaught exception',
uncaughtException: 'Uncaught exception',
warning: getWarningHeader,
unhandledRejection: 'a promise was rejected but not handled',
rejectionHandled: 'a promise was handled after being already rejected',
multipleResolves: 'a promise was resolved/rejected multiple times',
unhandledRejection: 'A promise was rejected but not handled',
rejectionHandled: 'A promise was handled after being already rejected',
multipleResolves: 'A promise was resolved/rejected multiple times',
}

// Start the message with an icon followed by `Error` or `Warning`
// Also add colors
const prettifyHeader = function({ header, eventName }) {
if (eventName === 'warning') {
return yellow(`${bold(` ${WARN_SIGN} Warning`)} ${header}`)
}
const level = eventName === 'warning' ? 'warn' : 'error'
const { COLOR, SIGN } = LEVELS[level]

return red(`${bold(` ${ERROR_SIGN} Error`)}: ${header}`)
return COLOR(`${bold(inverse(` ${SIGN} ${eventName} `))} ${header}`)
}

const isWindows = platform === 'win32'
const ERROR_SIGN = isWindows ? '\u00D7' : '\u2718'
const WARN_SIGN = isWindows ? '\u203C' : '\u26A0'
const LEVELS = {
warn: {
COLOR: yellow,
SIGN: isWindows ? '\u203C' : '\u26A0',
},
error: {
COLOR: red,
SIGN: isWindows ? '\u00D7' : '\u2718',
},
}

const getContent = function({ promiseState, promiseValue, error }) {
const getContent = function({
promiseState,
promiseValue,
secondPromiseState,
secondPromiseValue,
error,
}) {
const content =
promiseState === undefined
? printError(error)
: getPromiseContent({ promiseState, promiseValue })
? printValue(error)
: getPromiseContent({
promiseState,
promiseValue,
secondPromiseState,
secondPromiseValue,
})
const contentA = indentContent(content)
return contentA
}

// `unhandledRejection`, `rejectionHandled` and `multipleResolves` events show
// the promise's resolved/rejected state and value in `message`
const getPromiseContent = function({ promiseState, promiseValue }) {
const value = printError(promiseValue)
const separator = promiseValue instanceof Error ? '\n' : ' '
return `Promise was ${promiseState} with:${separator}${value}`
const getPromiseContent = function({
promiseState,
promiseValue,
secondPromiseState,
secondPromiseValue,
}) {
if (secondPromiseState === undefined) {
return `Promise was ${promiseState} with: ${serialize(promiseValue)}`
}

const again = promiseState === secondPromiseState ? ' again' : ''
return `Promise was initially ${promiseState} with: ${serialize(promiseValue)}
Promise was then ${secondPromiseState}${again} with: ${serialize(
secondPromiseValue,
)}`
}

// Print `Error.name|message|stack` if it's an `Error`. Stringify otherwise.
const printError = function(error) {
if (!(error instanceof Error)) {
return String(error)
const serialize = function(value) {
const valueA = printValue(value)
// Print multiline values on the next line
const valueB = valueA.includes('\n') ? `\n${valueA}` : valueA
return valueB
}

const printValue = function(value) {
if (value instanceof Error) {
return printError(value)
}

const { name, message, stack } = error
return String(value)
}

// Print `Error.name|message|stack` if it's an `Error`. Stringify otherwise.
const printError = function({ name, message, stack }) {
if (stack.startsWith(name)) {
return stack
}
Expand Down

0 comments on commit 5530e66

Please sign in to comment.