avajs/ava

Add Error .cause property to output

Open

#3186 opened on Mar 21, 2023

View on GitHub
 (5 comments) (2 reactions) (1 assignee)JavaScript (20,600 stars) (1,408 forks)batch import
enhancementhelp wantedscope:reporters

Description

Ava does not currently output an information related the Error.cause property.

Error.cause is somewhat new with support in NodeJS 16+, Deno 1.13+, and major browsers since ~September 2021. The .cause property can contain another Error which is useful when tracing re-throws. But, it can be of any type. It's currently supported .

When you console.log such an Error with .cause of another Error in the options, you get something like the following

image

Welcome to Node.js v18.12.0.
Type ".help" for more information.
> function test_a(){ throw new Error('test', { cause: new Error('test cause') }); }
undefined
> test_a()
Uncaught Error: test
    at test_a (REPL1:1:26) {
  [cause]: Error: test cause
      at test_a (REPL1:1:53)
      at REPL2:1:1
      at Script.runInThisContext (node:vm:129:12)
      at REPLServer.defaultEval (node:repl:572:29)
      at bound (node:domain:433:15)
      at REPLServer.runBound [as eval] (node:domain:444:12)
      at REPLServer.onLine (node:repl:902:10)
      at REPLServer.emit (node:events:525:35)
      at REPLServer.emit (node:domain:489:12)
      at [_onLine] [as _onLine] (node:internal/readline/interface:425:12)
}
>

When .cause is just structured data, a string representation of that data is output after [cause]:

For comparison, in Ava, when you throw an error with a cause, the cause is omitted

image

  FnCaster › this is a test of error cause

  test/FnCaster.ts:51

   50:   function test_a(){                                 
   51:     throw new Error('test', { cause: 'test_cause' });
   52:   }                                                  

  Error thrown in test:

  Error {
    message: 'test',
  }

  › test_a (file://test/FnCaster.ts:51:11)
  › file://test/FnCaster.ts:53:3

  ─

It would be nice for Ava to show the .cause in some form.

Contributor guide