Print current stack trace in JavaScript
This line of code gets a stack trace and prints it:
console.trace();
Source: https://developer.mozilla.org/en-US/docs/Web/API/Console/trace
Get the entire stack trace in Node
In your code, you need to declare:
Error.stackTraceLimit = Infinity;
This works for both methods in the OP.
NodeJS - Print stack trace when stuck/frozen
You are looking for checking if event loop is blocked or slow. There is a npm package https://www.npmjs.com/package/blocked-at that detects slow synchronous execution and report where it started.
Usage:
const blocked = require('blocked-at');
blocked((time, stack) => {
console.log(`Blocked for ${time}ms, operation started here:`, stack)
});
from scratch you can implement yourself a check in this way:
var interval = 500;
var interval = setInterval(function() {
var last = process.hrtime();
setImmediate(function() {
var delta = process.hrtime(last);
if (delta > blockDelta) {
console.log("node.eventloop_blocked", delta);
}
});
}, interval);
The idea is: if the timer doesn't fire after the expected time, this mean that event loop was blocked in some operation.
This snippet check if event loop is blocked for more than 500 ms. Isn't perfect, I'm suggest to use blocked-at for more robust control.
is there a way to print the stacktrace of the current JS line when I ctrl+c in terminal?
The Node runtime had the --trace-sigint
argument added in May 2020 (as part of nodejs/node#29207
for this very purpose.
When running through npx
, its --node-options
flag can be used to pass this option on to the runtime like so:
npx --node-options='--trace-sigint' ts-node code.ts
How to log stack traces in node.js
You can get this text off of the .stack
property from any Error. For instance:
try {
throw new Error();
} catch (e) {
console.log(e.stack);
}
or just new
up an error for the purposes of getting the stack trace
console.log(new Error().stack)
How to output a deep stack trace in node.js?
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)
*/
Node - How to reconstruct full stack trace for Promise rejection
I would use an async function here exactly for the reason of preserving the stack trace:
async function myFunction() {
await fetch('https://www.google.com');
throw new Error('Whoops!');
}
async function foo() {
await myFunction();
}
async function bar() {
await foo();
}
bar().catch(console.log);
Don't be bothered by the process.processTicksAndRejections
entry in the stack trace. As long as async/await are used consistently, the whole call chain should be logged with the exception.
Related Topics
How to Check Whether <Ng-Content> Is Empty? (In Angular 2+ Till Now)
How Is JavaScript Single Threaded
What's the Difference Between JavaScript and Jscript
Puppeteer Page.Evaluate Queryselectorall Return Empty Objects
Truncate (Not Round Off) Decimal Numbers in JavaScript
Indirect Function Call in JavaScript
Jquery Function to Get All Unique Elements from an Array
Access Object Properties Within Object
JavaScript - Difference Between Array and Array-Like Object
Inject a Script Tag with Remote Src and Wait for It to Execute
Selecting an Element in Iframe with Jquery
How to Filter JSON Data in JavaScript or Jquery
How to Store a JavaScript Function in JSON
JavaScript to Sort Contents of Select Element
How to Disable Signature Checking for Firefox Add-Ons
What Is Browser.Ignoresynchronization in Protractor
Jquery Difference Between Change and Click Event of Checkbox