arcanis / clipanion

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

default `Internal Error` formatting of uncaught errors drops `cause` #159

Open seansfkelley opened 5 months ago

seansfkelley commented 5 months ago

In ES2022 (I think?), Errors gained a cause attribute. The default behavior of Clipanion to format the error doesn't check for this field, only stack, so the causes of rethrown errors are silently dropped.

I'd be happy to open a PR to add cause support to the default formatting behavior. I would have already but to make it typesafe I would have to change the tsconfig.json's lib it compiles against (and potentially the TypeScript version itself?), and I don't know what compatibility requirements you have. cause is supported since Node 16, which is now EOL.

seansfkelley commented 5 months ago

Here's a slapdash implementation I patched in with yarn patch to my local installation so you can get a sense for what support might look like:

function recursivelyAppendCause(error, indent) {
    if (error) {
        if (error.stack) {
            result += `${indent}[cause]: ${error.stack.split('\n').map(l => `${indent}${l}`).join('\n').replace(/^\s*/, '')}\n`;
        } else {
            result += `${indent}[cause]: [non-error object]\n${error}\n`;
        }
        recursivelyAppendCause(error.cause, indent + '  ');
    }
}

recursivelyAppendCause(error.cause, '  ');

(in Cli#error).