2

I'm working with a library that obfuscates errors by throwing an object directly - I believe.

//Functions a,b, b2, c are all in a library that I don't have control over.
function a() {
  console.log("a");
}

function b() {
  throw new Error("You can see my stacktrace!");
}

function b2() {
  throw "I could have come from anywhere!";
}

function c() {
  console.log("c");
}

//Our code here.
function all() {
  try {
    console.log("Start all()");
    a();
    b();
    c();
  } catch (err) {
    console.error(err);
  }
}

function all2() {
  try {
    console.log("Start all2()");
    a();
    b2();
    c();
  } catch (err) {
    console.error(err);
  }
}

all();
all2();

Code Sandbox

Output:

[nodemon] starting `node src/index.js localhost 8080`
Start all()
a
Error: You can see my stacktrace!
    at b (/sandbox/src/index.js:7:9)
    at all (/sandbox/src/index.js:23:5)
    at Object.<anonymous> (/sandbox/src/index.js:41:1)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at startup (internal/bootstrap/node.js:283:19)
Start all2()
a
I could have come from anywhere!
[nodemon] clean exit - waiting for changes before restart

The problem is this makes it difficult for me to debug my code - I don't know if the error is coming from function a(), b(), or c().

Is there a tidy way to reintroduce the stack trace?

The best option I've got so far looks like this:

function wrapAndRunFunction(fn) {
  try {
    fn();
  } catch (err) {
    throw new Error(err);
  }
}

function all3() {
  try {
    console.log("Start all3()");
    wrapAndRunFunction(a);
    wrapAndRunFunction(b2);
    wrapAndRunFunction(c);
  } catch (err) {
    console.error(err);
  }
}
Start all3()
a
Error: I could have come from anywhere!
    at wrapAndRunFunction (/sandbox/src/index.js:45:11)
    at all3 (/sandbox/src/index.js:53:5)
    at Object.<anonymous> (/sandbox/src/index.js:62:1)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at startup (internal/bootstrap/node.js:283:19)
[nodemon] clean exit - waiting for changes before restart
dwjohnston
  • 11,163
  • 32
  • 99
  • 194
  • What library is this? – Avin Kavish Jun 18 '19 at 04:37
  • @AvinKavish - https://github.com/hyperledger/indy-sdk – dwjohnston Jun 18 '19 at 04:49
  • They aren't exactly trying to hide line numbers as far as I can see. They have their own error object called [IndyError](https://github.com/hyperledger/indy-sdk/blob/master/wrappers/nodejs/src/IndyError.js) that they throw on any library error. It may not be setup properly to expose line numbers. Can you show me an original stack trace you encountered? This might be worth a bug report. They have mentioned the nodeJs api to be experimental after all – Avin Kavish Jun 18 '19 at 05:03

0 Answers0