Skip to content

Feature Request: Error cause chaining with error() and fatal() #316

@PierreBeucher

Description

@PierreBeucher

Considering code using new Error("xxx", { cause: error }) to chain error, for example:

function a() {
    throw new Error("Oops")
}

function b() {
    try {
        a()
    } catch (error) {
        throw new Error("Something's wrong", { cause: error })
    }
}

function c() {
    try {
        b()
    } catch (error) {
        throw new Error("Well, that's not good", { cause: error })
    }
}

try {
    c()
} catch (error) {
    new Logger().error("Error with stack", error)
    console.error(error)
}

Using plain console.error() outputs the error and all its causes which is very practical for debugging. Something this:

Error: Well, that's not good
    at c (/home/pbeucher/git/cloudypad/saas/api/src/test2.ts:21:15)
    at <anonymous> (/home/pbeucher/git/cloudypad/saas/api/src/test2.ts:26:5)
    ... 6 lines matching cause stack trace ...
    at wrapModuleLoad (node:internal/modules/cjs/loader:219:24)
    at cjsLoader (node:internal/modules/esm/translators:263:5) {
  [cause]: Error: Something's wrong
      at b (/home/pbeucher/git/cloudypad/saas/api/src/test2.ts:13:15)
      at c (/home/pbeucher/git/cloudypad/saas/api/src/test2.ts:19:9)
      ... 6 lines matching cause stack trace ...
      at TracingChannel.traceSync (node:diagnostics_channel:322:14)
      at wrapModuleLoad (node:internal/modules/cjs/loader:219:24) {
    [cause]: Error: Oops
        at a (/home/pbeucher/git/cloudypad/saas/api/src/test2.ts:6:11)
        at b (/home/pbeucher/git/cloudypad/saas/api/src/test2.ts:11:9)
        at c (/home/pbeucher/git/cloudypad/saas/api/src/test2.ts:19:9)
        at <anonymous> (/home/pbeucher/git/cloudypad/saas/api/src/test2.ts:26:5)
        at Object.<anonymous> (/home/pbeucher/git/cloudypad/saas/api/src/test2.ts:30:1)
        at Module._compile (node:internal/modules/cjs/loader:1565:14)
        at Object.transformer (/home/pbeucher/git/cloudypad/saas/api/node_modules/.pnpm/[email protected]/node_modules/tsx/dist/register-DCnOAxY2.cjs:2:1186)
        at Module.load (node:internal/modules/cjs/loader:1318:32)
        at Function._load (node:internal/modules/cjs/loader:1128:12)
        at TracingChannel.traceSync (node:diagnostics_channel:322:14)

But by default tslog shows something like this:

2025-04-16 18:03:58.356 ERROR   /src/test2.ts:28        example Error with stack

 Error  Well, that's not good, Error: Something's wrong
error stack:
  • test2.ts    c
        /src/test2.ts:21
  • test2.ts    <anonymous>
        /src/test2.ts:26
  • test2.ts    Object.<anonymous>
        /src/test2.ts:30
  • loader      Module._compile
        internal/modules/cjs/loader:1565
  • register-DCnOAxY2.cjs       Object.transformer
        /node_modules/.pnpm/[email protected]/node_modules/tsx/dist/register-DCnOAxY2.cjs:2
  • loader      Module.load
        internal/modules/cjs/loader:1318
  • loader      Function._load
        internal/modules/cjs/loader:1128
  • diagnostics_channel TracingChannel.traceSync
        diagnostics_channel:322
  • loader      wrapModuleLoad
        internal/modules/cjs/loader:219
  • translators cjsLoader
        internal/modules/esm/translators:263

Is there a way to show error causes and chain them ? If not, can we configure it or have it as feature ? It would be great ! Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions