You know that feeling of disapointment when you get an error and the stack trace just abruptly ends and you know for a fact it should be much longer, and much more useful than that?
1
2
3
|
Error: Error message at null ._onTimeout (/examples/error-module.js:7:29) at Timer.listOnTimeout [as ontimeout] (timers.js:110:15) |
I’m very well familiar with that feeling… Maybe too much? Turns out there’s a way to get much better results in Node with a little bit of magic. Said magic is a module called longjohn by Matt Insler.
1
|
npm install longjohn |
Usage
Using longjohn couldn’t get any easier. You just need to require('longjohn')
and you are all set. The magic that I mentioned above consists of replacing functions like setTimeout
and nextTick
with patched versions that keep track of the stack as it unfolds and as author suggests, this probably shouldn’t be used in production.
But, after you require longjohn, your short, mildly helpful stack like this:
1
2
3
|
Error: Error message at null ._onTimeout (/examples/error-module.js:7:29) at Timer.listOnTimeout [as ontimeout] (timers.js:110:15) |
Turns into this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
Error: Error message at [object Object].<anonymous> (/examples/error-module.js:7:29) at listOnTimeout (timers.js:110:15) --------------------------------------------- at delayed (/examples/error-module.js:3:3) at delayedError (/examples/error-module.js:7:3) at [object Object].<anonymous> (/examples/error-module.js:21:5) at listOnTimeout (timers.js:110:15) --------------------------------------------- at delayed (/examples/error-module.js:3:3) at module.exports (/examples/error-module.js:20:3) at Object.<anonymous> (/examples/longjohn.js:2:26) at Module._compile (module.js:456:26) at Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Module._load (module.js:312:12) at Module.runMain (module.js:497:10) |
Closing thoughts
Getting a full stack is imperative to finding out the root cause of the problem in your code. longjohn addresses that on the Node side. There’s a similiar module that works in the browser called zone.js by Brian Ford that I think you should check out as well.
Check out example for this article on GitHub.