Skip to content

Commit de28840

Browse files
committed
Improve prettify
1 parent a993f6a commit de28840

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/message.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { inspect } = require('util')
44

5-
const { serialize, prettify } = require('./serialize')
5+
const { serialize, prettify } = require('./prettify')
66

77
// Retrieve `message` which sums up all information that can be gathered about
88
// the event.
@@ -37,22 +37,25 @@ const defaultGetMessage = function({
3737
}
3838

3939
const uncaughtException = function({ error }) {
40-
return `This exception was thrown but not caught: ${serialize(error)}`
40+
return `an exception was thrown but not caught
41+
${serialize(error)}`
4142
}
4243

43-
const warning = function({ error, error: { code, detail = '' } }) {
44+
const warning = function({ error, error: { code, detail } }) {
4445
const codeMessage = code === undefined ? '' : `(${code}) `
45-
return `${codeMessage}${detail}${serialize(error)}`
46+
const detailMessage = detail === undefined ? '' : `${detail}\n`
47+
return `
48+
${codeMessage}${detailMessage}${serialize(error)}`
4649
}
4750

4851
const unhandledRejection = function({ promiseValue }) {
49-
return `This promise was rejected but not handled: ${serialize(promiseValue)}`
52+
return `a promise was rejected but not handled
53+
${serialize(promiseValue)}`
5054
}
5155

5256
const rejectionHandled = function({ promiseValue }) {
53-
return `This promise was rejected and handled too late: ${serialize(
54-
promiseValue,
55-
)}`
57+
return `a promise was rejected and handled too late
58+
${serialize(promiseValue)}`
5659
}
5760

5861
const multipleResolves = function({
@@ -62,12 +65,9 @@ const multipleResolves = function({
6265
secondPromiseValue,
6366
}) {
6467
const again = promiseState === secondPromiseState ? ' again' : ''
65-
return `A promise was resolved/rejected multiple times. It was initially ${promiseState} with: ${serialize(
66-
promiseValue,
67-
)}
68-
It was then ${secondPromiseState}${again} with: ${serialize(
69-
secondPromiseValue,
70-
)}`
68+
return `a promise was resolved/rejected multiple times
69+
Initially ${promiseState} with: ${serialize(promiseValue)}
70+
Then ${secondPromiseState}${again} with: ${serialize(secondPromiseValue)}`
7171
}
7272

7373
const MESSAGES = {

src/serialize.js renamed to src/prettify.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,25 @@ const prettify = function({
1717
eventName,
1818
level,
1919
colors,
20-
colors: { bold, dim, inverse },
20+
colors: { bold, dim, inverse, italic },
2121
}) {
22-
const [header, ...lines] = message.split('\n')
22+
const [explanation, firstLine, ...lines] = message.split('\n')
2323

24-
// Add color, icon and `eventName` to first message line.
24+
// `warning` events do not have an `explanation`
25+
const explanationA =
26+
explanation === '' ? '' : ` ${italic(`(${explanation})`)}`
27+
28+
// Add color, sign and `eventName` to first message line, and concatenate
29+
// `firstLine`
2530
const { COLOR, SIGN } = LEVELS[level]
26-
const headerA = colors[COLOR](
27-
`${bold(inverse(` ${SIGN} ${eventName} `))} ${header}`,
31+
const header = colors[COLOR](
32+
`${inverse(bold(` ${SIGN} ${eventName}${explanationA} `))} ${firstLine}`,
2833
)
34+
2935
// Add gray color and indentation to other lines.
3036
const linesA = lines.map(line => dim(`${INDENT}${line}`))
3137

32-
const messageA = [headerA, ...linesA].join('\n')
38+
const messageA = [header, ...linesA].join('\n')
3339
return messageA
3440
}
3541

0 commit comments

Comments
 (0)