Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output a deep stack trace in node.js?

Tags:

When a normal exception occurs, a stack trace like the following is outputted:

util.js:38       case '%s': return String(args[i++]);                         ^ TypeError: Cannot convert object to primitive value     at String (unknown source)     at util.js:38:25     at String.replace (native)     at Object.<anonymous> (util.js:35:23)     at Object.<anonymous> (console.js:25:36)     at EventEmitter.<anonymous> (/project/src/routines/debug/boot.js:16:21)     at EventEmitter.emit (/project/node_modules/eventemitter2/lib/eventemitter2.js:319:22)     at /project/src/bootstrap.js:15:14     at /project/src/util/routineloader.js:36:11     at /project/src/util/routineloader.js:47:6 

Which is very helpful. When I then do the following somewhere:

process.on('uncaughtException', function(err) {         console.trace();         throw err;     }); 

I only get:

Trace:      at EventEmitter.<anonymous> (/project/src/routines/debug/exceptions.js:4:17)     at EventEmitter.emit (events.js:88:20) 

Which is not helpful at all.

How do I make it return the entire stack trace, like the original one?

like image 336
Tom Avatar asked May 10 '12 17:05

Tom


People also ask

How do I capture a stack trace?

You can obtain a stack trace from a thread – by calling the getStackTrace method on that Thread instance. This invocation returns an array of StackTraceElement, from which details about stack frames of the thread can be extracted.

What is node js stack trace?

The stack trace is used to trace the active stack frames at a particular instance during the execution of a program. The stack trace is useful while debugging code as it shows the exact point that has caused an error. Errors in Node.

What is full stack trace?

A full stack trace allows you to see the chain of files from when your custom tag reached your set breakpoint. If you click on any of the pages in the caller chain then FusionDebug will show you that page and will highlight the line at which the next page in the chain was called.

What is err in Node JS?

An error in Node. js is any instance of the Error object. Common examples include built-in error classes, such as ReferenceError , RangeError , TypeError , URIError , EvalError , and SyntaxError .


1 Answers

You're almost there:

process.on('uncaughtException', function(err) {   console.log(err.stack);   throw err; });  function foo() {   throw new Error("HI. I'm an error."); }  foo();  /* Prints Error: HI. I'm an error.     at foo (/Users/rye/Desktop/test.js:7:9)     at Object.<anonymous> (/Users/rye/Desktop/test.js:10:1)     at Module._compile (module.js:441:26)     at Object..js (module.js:459:10)     at Module.load (module.js:348:31)     at Function._load (module.js:308:12)     at Array.0 (module.js:479:10)     at EventEmitter._tickCallback (node.js:192:40) */ 
like image 182
Ryan Roemer Avatar answered Oct 12 '22 00:10

Ryan Roemer