Node weird stack trace

Viewed 111

If I execute the following code :

"use strict";

const config = {};
const run = (fn, params) => fn(params);

run(
    params => {
        throw new Error("Simple test!");
    },
    { config }
);

I get the following stack trace:

Error: Simple test!
    at run.config.config (C:\test\test.js:10:9)
    at run (C:\test\test.js:6:29)
    at Object.<anonymous> (C:\test\test.js:8:1)
    at Module._compile (internal/modules/cjs/loader.js:1133:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at internal/main/run_main_module.js:18:47

Is it normal to have run.config.config or is it a bug?

2 Answers

{config} creates an object with property config from the original config.

The stack seems to be: config from parameter {config}. So "run.{config}.config" from run, apparently displayed as run.config.config.

The stack trace is different if fn is assigned earlier (so, when fn is not anonymous).

const config = {foo: 1, bar: 2};
const run = (fn, config) => {
  fn(config);
};

const runFn = params => { 
    console.log(`current params: ${JSON.stringify(params)}`); 
    throw new Error("Caught ya'");
}

run(runFn, {config});

Nodejs output with fn parameter as predefined named function (above snippet):

current params: {"config":{"foo":1,"bar":2}}
[...]\SO65523965.js:8
    throw new Error("Caught ya'");
    ^

Error: Caught ya'
    at runFn ([...]\SO65523965.js:8:11)
    at run ([...]\SO65523965.js:3:3)
    ...

Nodejs output with inline fn parameter:

current params: {"config":{"foo":1,"bar":2}}
[...]\SO65523965.js:8
    throw new Error("Caught ya'");
    ^

Error: Caught ya'
    at run.config.config ([...]\SO65523965.js:13:11)
    at run ([...]\SO65523965.js:3:3)

Is it normal to have run.config.config or is it a bug?

if you are pointing out that the config object shouldn't be mentioned in the stack trace since it's not related to the error, the yeah that's weird, in the other hand if you are wondering about the config.config, then i suspect that:

thanks to the Shorthand property names syntax this:

const a = 1, b = 2, c = 3;
const obj = {a, b, c};

is equivalent to this:

const a = 1, b = 2, c = 3;
const obj = {a:a, b:b, c:c};

in the same way that, this:

run(params => {
        throw new Error("Simple test!");
    },
    { config }
);

is equivalent to:

run(params => {
        throw new Error("Simple test!");
    },
    { config: config }
);

which could explain the config.config part, the first config seem to refer to the object { config: config } and the second (.config) refer to the config property name of the mentioned object

Related