arcanis / clipanion

Type-safe CLI library / framework with no runtime dependencies
https://mael.dev/clipanion/
1.1k stars 61 forks source link

The `cause` property of an error isn't included in the output #157

Open merceyz opened 7 months ago

merceyz commented 7 months ago

When an error is thrown the cause property is not included in the output

import { runExit, Command } from 'clipanion';

runExit(
    class extends Command {
        async execute() {
            throw new Error(`foo`, { cause: new Error(`bar`) });
        }
    },
);

Output:

Internal Error: foo
    at Command.execute (file:///tmp/tmp.v2V9v1f760/test.mjs:6:10)
    at Command.validateAndExecute (/tmp/tmp.v2V9v1f760/node_modules/clipanion/lib/advanced/Command.js:73:37)
    at /tmp/tmp.v2V9v1f760/node_modules/clipanion/lib/advanced/Cli.js:229:53
    at noopCaptureActivator (/tmp/tmp.v2V9v1f760/node_modules/clipanion/lib/advanced/Cli.js:457:12)
    at Cli.run (/tmp/tmp.v2V9v1f760/node_modules/clipanion/lib/advanced/Cli.js:229:30)
    at Cli.runExit (/tmp/tmp.v2V9v1f760/node_modules/clipanion/lib/advanced/Cli.js:238:39)
    at runExit (/tmp/tmp.v2V9v1f760/node_modules/clipanion/lib/advanced/Cli.js:16:16)
    at file:///tmp/tmp.v2V9v1f760/test.mjs:3:1
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:336:24)

For comparison this is what Node.js prints:

file:///tmp/tmp.v2V9v1f760/test.mjs:1
throw new Error(`foo`, {cause: new Error(`bar`)});
      ^

Error: foo
    at file:///tmp/tmp.v2V9v1f760/test.mjs:1:7
    at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
    ... 2 lines matching cause stack trace ...
    at async handleMainPromise (node:internal/modules/run_main:106:12) {
  [cause]: Error: bar
      at file:///tmp/tmp.v2V9v1f760/test.mjs:1:32
      at ModuleJob.run (node:internal/modules/esm/module_job:195:25)
      at async ModuleLoader.import (node:internal/modules/esm/loader:336:24)
      at async loadESM (node:internal/process/esm_loader:34:7)
      at async handleMainPromise (node:internal/modules/run_main:106:12)
}

Node.js v18.19.0

Ref https://github.com/nodejs/corepack/pull/365 where I ran into this.