Thanks to visit codestin.com
Credit goes to github.com

Skip to content

πŸš€ Feature: Add support for outputting Error causeΒ #2735

@TazmanianDI

Description

@TazmanianDI

We are using VError to gather up nested traces which is something proving to be very useful with promises and generators (we can actually get complete stack traces that way). However the default reporting with Mocha only outputs the first error in the stack.

For example, with this code:

const VError = require('verror');

describe('Test', () => {
  it('Test', () => {
    function1();
  });
});

function function1() {
  try {
    function2();
  } catch (e) {
    throw new VError(e);
  }
}

function function2() {
  throw new Error('Something Bad');
}

You get the output:

 VError: : Something Bad
  at function1 (test\sandbox.js:13:11)
  at Context.it (test\sandbox.js:5:5)
  at Test.Runnable.run (node_modules\co-mocha\lib\co-mocha.js:43:16)

This doesn't actually tell me where the root error came from (in function2)

If I add a try...catch and then console.error(e) the top level error, I got a lot more detail:

{ VError: : Something Bad
    at function1 (C:\Work\NPS\nps-dev\test\sandbox.js:17:11)
    at Context.it (C:\Work\NPS\nps-dev\test\sandbox.js:6:7)
    at callFn (C:\Work\NPS\nps-dev\node_modules\mocha\lib\runnable.js:343:21)
    at Test.Runnable.run (C:\Work\NPS\nps-dev\node_modules\mocha\lib\runnable.js:335:7)
    at Test.Runnable.run (C:\Work\NPS\nps-dev\node_modules\co-mocha\lib\co-mocha.js:43:16)
    at Runner.runTest (C:\Work\NPS\nps-dev\node_modules\mocha\lib\runner.js:444:10)
    at C:\Work\NPS\nps-dev\node_modules\mocha\lib\runner.js:550:12
    at next (C:\Work\NPS\nps-dev\node_modules\mocha\lib\runner.js:361:14)
    at C:\Work\NPS\nps-dev\node_modules\mocha\lib\runner.js:371:7
    at next (C:\Work\NPS\nps-dev\node_modules\mocha\lib\runner.js:295:14)
    at Immediate.<anonymous> (C:\Work\NPS\nps-dev\node_modules\mocha\lib\runner.js:339:5)
    at runCallback (timers.js:649:20)
    at tryOnImmediate (timers.js:622:5)
    at processImmediate [as _immediateCallback] (timers.js:594:5)
  jse_shortmsg: '',
  jse_cause: 
   Error: Something Bad
       at function2 (C:\Work\NPS\nps-dev\test\sandbox.js:22:9)
       at function1 (C:\Work\NPS\nps-dev\test\sandbox.js:15:5)
       at Context.it (C:\Work\NPS\nps-dev\test\sandbox.js:6:7)
       at callFn (C:\Work\NPS\nps-dev\node_modules\mocha\lib\runnable.js:343:21)
       at Test.Runnable.run (C:\Work\NPS\nps-dev\node_modules\mocha\lib\runnable.js:335:7)
       at Test.Runnable.run (C:\Work\NPS\nps-dev\node_modules\co-mocha\lib\co-mocha.js:43:16)
       at Runner.runTest (C:\Work\NPS\nps-dev\node_modules\mocha\lib\runner.js:444:10)
       at C:\Work\NPS\nps-dev\node_modules\mocha\lib\runner.js:550:12
       at next (C:\Work\NPS\nps-dev\node_modules\mocha\lib\runner.js:361:14)
       at C:\Work\NPS\nps-dev\node_modules\mocha\lib\runner.js:371:7
       at next (C:\Work\NPS\nps-dev\node_modules\mocha\lib\runner.js:295:14)
       at Immediate.<anonymous> (C:\Work\NPS\nps-dev\node_modules\mocha\lib\runner.js:339:5)
       at runCallback (timers.js:649:20)
       at tryOnImmediate (timers.js:622:5)
       at processImmediate [as _immediateCallback] (timers.js:594:5),
  jse_info: {},
  message: ': Something Bad' }

Of course, this is incredibly verbose but I would prefer this over less. I suspect this is because of the features that have been implemented that clean up the stack traces. I don't see why those couldn't run recursively and clean up all the nested stacks and output something more like this:

{ VError: : Something Bad
    at function1 (C:\Work\NPS\nps-dev\test\sandbox.js:17:11)
    at Context.it (C:\Work\NPS\nps-dev\test\sandbox.js:6:7)
    at Test.Runnable.run (C:\Work\NPS\nps-dev\node_modules\co-mocha\lib\co-mocha.js:43:16)
   Error: Something Bad
       at function2 (C:\Work\NPS\nps-dev\test\sandbox.js:22:9)
       at function1 (C:\Work\NPS\nps-dev\test\sandbox.js:15:5)
       at Context.it (C:\Work\NPS\nps-dev\test\sandbox.js:6:7)
       at Test.Runnable.run (C:\Work\NPS\nps-dev\node_modules\co-mocha\lib\co-mocha.js:43:16)

Now I can see the original exception in function2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions