11'use strict'
22
3- const { inspect } = require ( 'util' )
4-
5- const { bold, dim, inverse } = require ( 'chalk' )
6-
7- const { LEVELS } = require ( './level' )
3+ const { printMultiline, print, prettify } = require ( './serialize' )
84
95// Retrieve `message` which sums up all information that can be gathered about
106// the event.
@@ -31,23 +27,23 @@ const getMessage = function({
3127
3228const uncaughtException = function ( { error } ) {
3329 return `An exception was thrown but not caught
34- ${ printValue ( error ) } `
30+ ${ print ( error ) } `
3531}
3632
3733const warning = function ( { error, error : { code, detail = '' } } ) {
3834 const codeMessage = code === undefined ? '' : `(${ code } ) `
3935 return `${ codeMessage } ${ detail }
40- ${ printValue ( error ) } `
36+ ${ print ( error ) } `
4137}
4238
4339const unhandledRejection = function ( { promiseValue } ) {
4440 return `A promise was rejected but not handled
45- Promise was rejected with: ${ serialize ( promiseValue ) } `
41+ Promise was rejected with: ${ printMultiline ( promiseValue ) } `
4642}
4743
4844const rejectionHandled = function ( { promiseValue } ) {
4945 return `A promise was handled after being already rejected
50- Promise was rejected with: ${ serialize ( promiseValue ) } `
46+ Promise was rejected with: ${ printMultiline ( promiseValue ) } `
5147}
5248
5349const multipleResolves = function ( {
@@ -58,8 +54,8 @@ const multipleResolves = function({
5854} ) {
5955 const again = promiseState === secondPromiseState ? ' again' : ''
6056 return `A promise was resolved/rejected multiple times
61- Promise was initially ${ promiseState } with: ${ serialize ( promiseValue ) }
62- Promise was then ${ secondPromiseState } ${ again } with: ${ serialize (
57+ Promise was initially ${ promiseState } with: ${ printMultiline ( promiseValue ) }
58+ Promise was then ${ secondPromiseState } ${ again } with: ${ printMultiline (
6359 secondPromiseValue ,
6460 ) } `
6561}
@@ -72,36 +68,6 @@ const MESSAGES = {
7268 multipleResolves,
7369}
7470
75- const serialize = function ( value ) {
76- const valueA = printValue ( value )
77- // Print multiline values on the next line
78- const valueB = valueA . includes ( '\n' ) ? `\n${ valueA } ` : valueA
79- return valueB
80- }
81-
82- const printValue = function ( value ) {
83- if ( value instanceof Error ) {
84- return value . stack
85- }
86-
87- return inspect ( value )
88- }
89-
90- const prettify = function ( { message, eventName, level } ) {
91- const [ header , ...lines ] = message . split ( '\n' )
92-
93- // Add color, icon and `eventName` to first message line.
94- const { COLOR , SIGN } = LEVELS [ level ]
95- const headerA = COLOR ( `${ bold ( inverse ( ` ${ SIGN } ${ eventName } ` ) ) } ${ header } ` )
96- // Add gray color and indentation to other lines.
97- const linesA = lines . map ( line => dim ( `\t${ VERTICAL_BAR } ${ line } ` ) )
98-
99- const messageA = [ headerA , ...linesA ] . join ( '\n' )
100- return messageA
101- }
102-
103- const VERTICAL_BAR = '\u2016'
104-
10571module . exports = {
10672 getMessage,
10773}
0 commit comments