Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I not seeing a stack trace with 'throw new Error'?

Why am I getting the following error with this simple code below when testing on a node server?

code/app2/i.js:35
        throw new Error("here")
              ^
Error: here

I'm actually expecting to see a stack trace as per the book 'Smashing node.js' (picture of the relevant page 35):

* node uncaught-http-js

/uncaught-http.js:4
  throw new Error("here");
        ^
Error: This will be uncaught
    at Server.<anonymous> (/uncaught-http.js:4:9)
    at Server.emit(events.js:70:17)
    at HttpParser.onIncoming(http.js:1514:12)
    at HttpParser.onHeadersComplete(http.js:102:31)
    at Socket.andata (http.js:1410:22)
    at TCP.onread(net.js:354:27)

but that's not happening.

Here's the code.

function c () {
    b();
};
function b () {
    a();
};

function a () {
    setTimeout(function () {
        throw new Error('here');
    }, 10);
};

c();
like image 336
Agent Zebra Avatar asked Dec 01 '22 00:12

Agent Zebra


2 Answers

You don't see the whole stack trace because you throw an error in A() asynchronously (using setTimeout). If you throw it synchronously - you'll see the trace c()->b()->a().

Try it: http://jsbin.com/yirorimewe/1/edit?js,console

like image 178
Marisev Avatar answered Dec 06 '22 12:12

Marisev


This is what your code does: It calls method 'c', which calls method 'b', which calls method 'a', which throws an error (inside method 'a' there is a throw error statement).

What you've posted is the stack trace (it shows you the line in the file where the error occurred).

like image 44
Yaniv Efraim Avatar answered Dec 06 '22 13:12

Yaniv Efraim