@@ -35,7 +35,18 @@ function bangify(msg: string, c: string): string {
35
35
return lines . join ( '\n' )
36
36
}
37
37
38
- export function getErrorMessage ( err : any ) : string {
38
+ export function getErrorMessage ( err : any , opts : { stack ?: boolean } = { } ) : string {
39
+ const severity = ( err [ 'cli-ux' ] && err [ 'cli-ux' ] . severity ) || 'error'
40
+ const context = err [ 'cli-ux' ] && err [ 'cli-ux' ] . context
41
+
42
+ function wrap ( msg : string ) : string {
43
+ const linewrap = require ( '@heroku/linewrap' )
44
+ return linewrap ( 6 , deps . screen . errtermwidth , {
45
+ skip : / ^ \$ .* $ / ,
46
+ skipScheme : 'ansi-color' ,
47
+ } ) ( msg )
48
+ }
49
+
39
50
let message
40
51
if ( err . body ) {
41
52
// API error
@@ -46,54 +57,43 @@ export function getErrorMessage(err: any): string {
46
57
}
47
58
}
48
59
// Unhandled error
49
- if ( err . message && err . code ) {
50
- message = `${ inspect ( err . code ) } : ${ err . message } `
51
- } else if ( err . message ) {
52
- message = err . message
53
- }
54
- const context = err [ 'cli-ux' ] && err [ 'cli-ux' ] . context
60
+ if ( err . message ) message = err . message
55
61
if ( context && ! _ . isEmpty ( context ) ) {
56
62
message += '\n' + indent ( styledObject ( err [ 'cli-ux' ] . context ) , 4 )
57
63
}
58
- return message || inspect ( err )
59
- }
60
-
61
- function displayError ( err : CLIError ) {
62
- function wrap ( msg : string ) : string {
63
- const linewrap = require ( '@heroku/linewrap' )
64
- return linewrap ( 6 , deps . screen . errtermwidth , {
65
- skip : / ^ \$ .* $ / ,
66
- skipScheme : 'ansi-color' ,
67
- } ) ( msg )
68
- }
69
-
70
- function render ( ) : string {
71
- const severity = ( err [ 'cli-ux' ] && err [ 'cli-ux' ] . severity ) || 'error'
72
- const msg = [
73
- _ . upperFirst ( severity === 'warn' ? 'warning' : severity ) ,
74
- ': ' ,
75
- getErrorMessage ( err ) ,
76
- ] . join ( '' )
77
- if ( process . env . CI || severity === 'fatal' || config . debug ) {
78
- // show stack trace
79
- let stack = err . stack || inspect ( err )
80
- stack = clean ( stack , { pretty : true } )
81
- stack = extract ( stack )
82
- return [ msg , stack ] . join ( '\n' )
83
- }
64
+ message = message || inspect ( err )
65
+
66
+ message = [
67
+ _ . upperFirst ( severity === 'warn' ? 'warning' : severity ) ,
68
+ ': ' ,
69
+ message ,
70
+ ] . join ( '' )
71
+
72
+ if ( opts . stack || process . env . CI || severity === 'fatal' || config . debug ) {
73
+ // show stack trace
74
+ let stack = err . stack || inspect ( err )
75
+ stack = clean ( stack , { pretty : true } )
76
+ stack = extract ( stack )
77
+ message = [ message , stack ] . join ( '\n' )
78
+ } else {
84
79
let bang = severity === 'warn' ? chalk . yellow ( arrow ) : chalk . red ( arrow )
85
80
if ( severity === 'warn' ) bang = chalk . yellow ( arrow )
86
- return bangify ( wrap ( msg ) , bang )
81
+ message = bangify ( wrap ( message ) , bang )
87
82
}
83
+ return message
84
+ }
85
+
86
+ function displayError ( err : CLIError ) {
87
+ const severity = ( err [ 'cli-ux' ] && err [ 'cli-ux' ] . severity ) || 'error'
88
88
89
89
function getBang ( ) : string {
90
- if ( err [ 'cli-ux' ] . severity === 'warn' ) return chalk . yellowBright ( '!' )
91
- if ( err [ 'cli-ux' ] . severity === 'fatal' ) return chalk . bold . bgRedBright ( '!!!' )
90
+ if ( severity === 'warn' ) return chalk . yellowBright ( '!' )
91
+ if ( severity === 'fatal' ) return chalk . bold . bgRedBright ( '!!!' )
92
92
return chalk . bold . redBright ( '!' )
93
93
}
94
94
95
95
config . action . pause ( ( ) => {
96
- console . error ( render ( ) )
96
+ console . error ( getErrorMessage ( err ) )
97
97
} , getBang ( ) )
98
98
}
99
99
0 commit comments