Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically capture Chrome Async Javascript stack trace

I've been working on trying to add some better error logging to a web application that is only run on Chrome. In essence, I want to be able to capture and store stack traces. For synchronous code, this works fine, but for async code, I've run into something a tad strange. Essentially, Chrome appears to log additional information as part of its async stack trace feature, but I haven't been able to figure out how to capture it.

Code, to run in Chrome browser console:

let e;
let a = () => Promise.resolve(null)
.then(() => (null).foo)
.catch(err => {
  console.info(err); 
  console.error(err); 
  e = err;
})
let b = () => a();
let c = () => b();
c();

Output:

(info)
TypeError: Cannot read property 'foo' of null
    at <anonymous>:3:20

(error, after expanding)
TypeError: Cannot read property 'foo' of null
    at <anonymous>:3:20
(anonymous) @ VM1963:6
Promise.catch (async)
a @ VM1963:4
b @ VM1963:9
c @ VM1963:10
(anonymous) @ VM1963:11

So console.error gives me a stack trace threaded all the way through the callstack, presumably through some form of Chrome engine magick. console.info gives me the actual stack trace that's stored on err. If after this is all done I attempt to read the value of e, its stack is the two lines I get from the console.info statement, not the console.error statement.

What I'm asking is, is there any way to capture and store the async stack trace that Chrome is generating and using when I call console.error?

like image 801
rmehlinger Avatar asked May 14 '19 17:05

rmehlinger


People also ask

How do I get stack trace on Google Chrome?

[Version 66.0. 3359.139 ]Go to Developer Tools -> Sources -> look on the right side(Call Stack). console. trace() // To print the call stack.

How do I debug async in Chrome?

# Enable async debugging in ChromeGo to the Sources panel of Chrome Canary DevTools. Next to the Call Stack panel on the right hand side, there is a new checkbox for "Async". Toggle the checkbox to turn async debugging on or off. (Although once it's on, you may not ever want to turn it off.)

How to View JavaScript call stack?

# View the current call stack. While paused on a line of code, use the Call Stack pane to view the call stack that got you to this point. If you're working with async code, check the Async checkbox to enable async call stacks. Click on an entry to jump to the line of code where that function was called.

What is an async stack trace?

Asynchronous stack traces allow you to inspect function calls beyond the current event loop. This is particularly useful because you can examine the scope of previously executed frames that are no longer on the event loop. This feature is currently an experiment and needs to be enabled.


1 Answers

console.error() seems to invoke console.trace() as a matter of convenience.

It doesn't look like a script can get the stack trace as anything other than a string. That string can be had with, for example, err.stack. MDN has documentation for the stack property of Error. It's not part of a spec but seems to be supported across all platforms.

like image 161
Ouroborus Avatar answered Oct 03 '22 15:10

Ouroborus