@@ -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.
99const 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
4952const isWindows = platform === 'win32'
5053const ERROR_SIGN = isWindows ? '×' : '\u2718'
5154const 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