Skip to content

Commit 556fa9a

Browse files
committed
Better message printing
1 parent 7c63039 commit 556fa9a

File tree

4 files changed

+38
-41
lines changed

4 files changed

+38
-41
lines changed

src/default.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
'use strict'
22

3-
// Default event handler
3+
// Default `opts.handlerFunc`
44
const defaultHandler = function({ eventName, message }) {
55
const level = eventName === 'warning' ? 'warn' : 'error'
66
// eslint-disable-next-line no-restricted-globals, no-console
77
console[level](message)
88
}
99

10+
const DEFAULT_OPTS = {
11+
handlerFunc: defaultHandler,
12+
exitOnExceptions: true,
13+
}
14+
1015
module.exports = {
11-
defaultHandler,
16+
DEFAULT_OPTS,
1217
}

src/handle.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const handleEvent = async function({
1313
promise,
1414
promiseValue,
1515
}) {
16+
debugger
1617
const { promiseState, promiseValue: promiseValueA } = await parsePromise({
1718
eventName,
1819
promise,

src/main.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const process = require('process')
44

5-
const { defaultHandler } = require('./default')
5+
const { DEFAULT_OPTS } = require('./default')
66
const EVENTS = require('./events')
77

88
// Add event handling for all process-related errors
@@ -14,11 +14,6 @@ const setup = function(opts) {
1414
return removeAll
1515
}
1616

17-
const DEFAULT_OPTS = {
18-
handlerFunc: defaultHandler,
19-
exitOnExceptions: true,
20-
}
21-
2217
const addListeners = function({ opts }) {
2318
return Object.entries(EVENTS).map(([eventName, eventFunc]) =>
2419
addListener({ opts, eventName, eventFunc }),

src/message.js

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,68 +7,64 @@ const { red, yellow, bold, dim } = require('chalk')
77
// Retrieve `message` which sums up all information that can be gathered about
88
// the event.
99
const getMessage = function({ eventName, promiseState, promiseValue, error }) {
10-
const mainMessage = getMainMessage({ eventName, error })
11-
const mainMessageA = prettify({ mainMessage, eventName })
10+
const header = getHeader({ eventName, error })
1211

13-
const secondMessage = getSecondMessage({ promiseState, promiseValue, error })
14-
const secondMessageA = indent(secondMessage)
12+
const content = getContent({ promiseState, promiseValue, error })
1513

16-
const message = `${mainMessageA}\n${dim(secondMessageA)}`
14+
const message = `${header}\n${dim(content)}`
1715
return message
1816
}
1917

2018
// First line of `message`
21-
const getMainMessage = function({ eventName, error }) {
22-
const mainMessage = MAIN_MESSAGES[eventName]
23-
24-
if (typeof mainMessage !== 'function') {
25-
return mainMessage
26-
}
27-
28-
return mainMessage({ error })
19+
const getHeader = function({ eventName, error }) {
20+
const header = HEADERS[eventName]
21+
const headerA = typeof header === 'function' ? header({ error }) : header
22+
const headerB = prettifyHeader({ header: headerA, eventName })
23+
return headerB
2924
}
3025

3126
// `warning` events use `Error.name|code|detail` in `message`
32-
const getWarningMessage = function({ error: { code, detail } }) {
27+
const getWarningHeader = function({ error: { code, detail } }) {
3328
const codeMessage = code === undefined ? '' : ` (${code})`
3429
const detailMessage = detail === undefined ? '' : `: ${detail}`
3530

3631
return `${codeMessage}${detailMessage}`
3732
}
3833

34+
const HEADERS = {
35+
uncaughtException: 'uncaught exception',
36+
warning: getWarningHeader,
37+
unhandledRejection: 'a promise was rejected but not handled',
38+
rejectionHandled: 'a promise was handled after being already rejected',
39+
multipleResolves: 'a promise was resolved/rejected multiple times',
40+
}
41+
3942
// Start the message with an icon followed by `Error` or `Warning`
4043
// Also add colors
41-
const prettify = function({ mainMessage, eventName }) {
44+
const prettifyHeader = function({ header, eventName }) {
4245
if (eventName === 'warning') {
43-
return yellow(`${bold(` ${WARN_SIGN} Warning`)} ${mainMessage}`)
46+
return yellow(`${bold(` ${WARN_SIGN} Warning`)} ${header}`)
4447
}
4548

46-
return red(`${bold(` ${ERROR_SIGN} Error`)}: ${mainMessage}`)
49+
return red(`${bold(` ${ERROR_SIGN} Error`)}: ${header}`)
4750
}
4851

4952
const isWindows = platform === 'win32'
5053
const ERROR_SIGN = isWindows ? '×' : '\u2718'
5154
const WARN_SIGN = isWindows ? '‼' : '\u26A0'
5255

53-
const MAIN_MESSAGES = {
54-
uncaughtException: 'uncaught exception',
55-
warning: getWarningMessage,
56-
unhandledRejection: 'a promise was rejected but not handled',
57-
rejectionHandled: 'a promise was handled after being already rejected',
58-
multipleResolves: 'a promise was resolved/rejected multiple times',
59-
}
60-
61-
const getSecondMessage = function({ promiseState, promiseValue, error }) {
62-
if (promiseState !== undefined) {
63-
return getPromiseMessage({ promiseState, promiseValue })
64-
}
65-
66-
return printError(error)
56+
const getContent = function({ promiseState, promiseValue, error }) {
57+
const content =
58+
promiseState === undefined
59+
? printError(error)
60+
: getPromiseContent({ promiseState, promiseValue })
61+
const contentA = indentContent(content)
62+
return contentA
6763
}
6864

6965
// `unhandledRejection`, `rejectionHandled` and `multipleResolves` events show
7066
// the promise's resolved/rejected state and value in `message`
71-
const getPromiseMessage = function({ promiseState, promiseValue }) {
67+
const getPromiseContent = function({ promiseState, promiseValue }) {
7268
const value = printError(promiseValue)
7369
const separator = promiseValue instanceof Error ? '\n' : ' '
7470
return `Promise was ${promiseState} with:${separator}${value}`
@@ -90,7 +86,7 @@ const printError = function(error) {
9086
}
9187

9288
// Indent each line
93-
const indent = function(string) {
89+
const indentContent = function(string) {
9490
return string.replace(EACH_LINE_REGEXP, `\t${VERTICAL_BAR} `)
9591
}
9692

0 commit comments

Comments
 (0)