From dba4afbed97071e05048613db3eb8d561049fb00 Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Tue, 20 Oct 2020 17:54:33 -0600 Subject: [PATCH] show error message with backtrace (if available) when --stacktrace option is set, even if stack property is missing --- CHANGELOG.adoc | 1 + packages/cli/lib/cli.js | 9 +++++++-- packages/cli/test/cli-test.js | 19 ++++++++++++++++++- .../fixtures/global-fail-tree-processor.js | 9 +++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 packages/cli/test/fixtures/global-fail-tree-processor.js diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 51a420d4e..1ef36a481 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -14,6 +14,7 @@ This project utilizes semantic versioning. === Fixed * *redirect-producer*: Add redirect modifier to splat alias rule for nginx (#698) +* *cli*: Show error message with backtrace (if available) when --stacktrace option is set, even if stack property is missing (#700) == 3.0.0-alpha.1 (2020-09-29) diff --git a/packages/cli/lib/cli.js b/packages/cli/lib/cli.js index b167f69d7..172a07e55 100644 --- a/packages/cli/lib/cli.js +++ b/packages/cli/lib/cli.js @@ -19,8 +19,13 @@ async function run (argv = process.argv) { } function exitWithError (err, showStack, msg = undefined) { - msg = showStack ? err.stack : `error: ${msg || err.message}\nAdd the --stacktrace option to see the cause.` - console.error(msg) + msg = `error: ${msg || err.message || err}` + // NOTE: the fallback for locating the stack can likely be removed after upgrading to Asciidoctor 2 + console.error( + showStack + ? err.stack || (err.backtrace ? [msg, ...err.backtrace.slice(1)].join('\n') : `${msg} (no stack)`) + : `${msg}\nAdd the --stacktrace option to see the cause.` + ) process.exit(1) } diff --git a/packages/cli/test/cli-test.js b/packages/cli/test/cli-test.js index b728eb8e4..a9abc48e8 100644 --- a/packages/cli/test/cli-test.js +++ b/packages/cli/test/cli-test.js @@ -243,6 +243,23 @@ describe('cli', function () { .done() }).timeout(timeoutOverride) + it('should show stack if --stacktrace option is specified and a Ruby exception without a stack property is thrown', () => { + playbookSpec.asciidoc = { attributes: { idseparator: 1 } } + fs.writeFileSync(playbookFile, toJSON(playbookSpec)) + return runAntora('--stacktrace generate the-site') + .assert(/^error: asciidoctor: FAILED: .* - undefined method `length' for 1/) + .assert(/^at Number\./) + .done() + }).timeout(timeoutOverride) + + it('should show message if --stacktrace option is specified and an exception with no stack is thrown', () => { + const ext = ospath.relative(WORK_DIR, ospath.join(FIXTURES_DIR, 'global-fail-tree-processor')) + fs.writeFileSync(playbookFile, toJSON(playbookSpec)) + return runAntora(`-r ${ext} --stacktrace generate the-site`) + .assert(/^error: not today! \(no stack\)/) + .done() + }).timeout(timeoutOverride) + it('should recommend --stacktrace option if not specified and an exception is thrown during generation', () => { playbookSpec.ui.bundle.url = false fs.writeFileSync(playbookFile, toJSON(playbookSpec)) @@ -553,7 +570,7 @@ describe('cli', function () { fs.writeFileSync(playbookFile, toJSON(playbookSpec)) // FIXME assert that exit code is 1 (limitation in Kapok when using assert) return runAntora('generate the-site') - .assert(/not found or failed to load/i) + .assert(/^error: Generator not found or failed to load/i) .on('exit', () => rmdirSync(localNodeModules)) .done() }) diff --git a/packages/cli/test/fixtures/global-fail-tree-processor.js b/packages/cli/test/fixtures/global-fail-tree-processor.js new file mode 100644 index 000000000..d536b3f96 --- /dev/null +++ b/packages/cli/test/fixtures/global-fail-tree-processor.js @@ -0,0 +1,9 @@ +'use strict' + +const asciidoctor = require('asciidoctor.js')() + +asciidoctor.Extensions.register(function () { + this.treeProcessor(function () { + throw 'not today!' + }) +}) -- GitLab