Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit baa6749

Browse files
committed
fix: show stack trace in errlog
1 parent eac6e87 commit baa6749

File tree

3 files changed

+44
-44
lines changed

3 files changed

+44
-44
lines changed

src/errors.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,18 @@ function bangify(msg: string, c: string): string {
3535
return lines.join('\n')
3636
}
3737

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+
3950
let message
4051
if (err.body) {
4152
// API error
@@ -46,54 +57,43 @@ export function getErrorMessage(err: any): string {
4657
}
4758
}
4859
// 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
5561
if (context && !_.isEmpty(context)) {
5662
message += '\n' + indent(styledObject(err['cli-ux'].context), 4)
5763
}
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 {
8479
let bang = severity === 'warn' ? chalk.yellow(arrow) : chalk.red(arrow)
8580
if (severity === 'warn') bang = chalk.yellow(arrow)
86-
return bangify(wrap(msg), bang)
81+
message = bangify(wrap(message), bang)
8782
}
83+
return message
84+
}
85+
86+
function displayError(err: CLIError) {
87+
const severity = (err['cli-ux'] && err['cli-ux'].severity) || 'error'
8888

8989
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('!!!')
9292
return chalk.bold.redBright('!')
9393
}
9494

9595
config.action.pause(() => {
96-
console.error(render())
96+
console.error(getErrorMessage(err))
9797
}, getBang())
9898
}
9999

src/logger.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as fs from 'fs-extra'
2-
import * as _ from 'lodash'
32
import * as path from 'path'
43

54
import {config} from './config'
@@ -43,16 +42,17 @@ export default (e: IEventEmitter) => {
4342

4443
const handleOutput = (m: Output.Message | Errors.Message) => {
4544
if (!canWrite(m.severity)) return
46-
const msg = m.type === 'error' ? Errors.getErrorMessage(m.error) : Output.render(m)
47-
const output = chomp(_([timestamp(), m.severity.toUpperCase(), msg]).compact().join(' '))
48-
buffer.push(deps.stripAnsi(output))
45+
let msg = m.type === 'error' ? Errors.getErrorMessage(m.error, {stack: true}) : Output.render(m)
46+
msg = deps.stripAnsi(chomp(msg))
47+
let lines = msg.split('\n').map(l => `${timestamp()} ${l}`)
48+
buffer.push(...lines)
4949
flush(50).catch(console.error)
5050
}
5151
e.on('output', handleOutput)
5252

5353
async function flush(waitForMs: number = 0) {
54+
await wait(waitForMs)
5455
flushing = flushing.then(async () => {
55-
await wait(waitForMs)
5656
if (!config.errlog || buffer.length === 0) return
5757
const file = config.errlog
5858
const mylines = buffer

test/logger.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('logger', () => {
2121
cli.info('hideme')
2222
cli.error('showerror', {exit: false})
2323
await cli.done()
24-
expect(fs.readFileSync(cli.config.errlog!, 'utf8')).to.contain(' ERROR showerror')
24+
expect(fs.readFileSync(cli.config.errlog!, 'utf8')).to.contain(' Warning: showwarning')
2525
})
2626

2727
fancy
@@ -34,7 +34,7 @@ describe('logger', () => {
3434
cli.debug('hideme')
3535
cli.error('showerror', {exit: false})
3636
await cli.done()
37-
expect(fs.readFileSync(cli.config.errlog!, 'utf8')).to.contain(' ERROR showerror')
37+
expect(fs.readFileSync(cli.config.errlog!, 'utf8')).to.contain(' Error: showerror')
3838
})
3939

4040
fancy

0 commit comments

Comments
 (0)