. Advertisement .
..3..
. Advertisement .
..4..
If you are still looking for possible methods to get the stack trace from an error in JavaScript, then consider reading this article. Below are solutions that we think might work for you. Let’s start with a quick look at some of the concepts!
What methods are to get the stack trace from an error in JavaScript?
Stack is a special type of linear list where addition and removal are always performed at one end (called the vertex). Or there is another definition: Stack is an abstract data structure that works on the principle of last in, first out (LIFO).
Stack trace, also known as the backtrace, is a list of stack frames. Each stack frame represents a program segment to be executed; they contain information about a method and the method that calls them. Thus, the Stack trace is a list of stack frames containing the list of methods called at the moment until they start. Like Stack, Stack trace also works on the principle of First in, last out (first in, first out). The first methods will be at the top of the stack frame, later methods will be added to the end of the stack frame, and a stack frame will be added and deleted when the called method finishes executing or returns a result.
Method 1: Use the console. trace() method
The first method we recommend is the console.trace() method to get the stack trace from an error in javascript.
Syntax:
console.trace(label)
Parameters
Label (Optional): A label to give the timer a name.
Sometimes you may want to log all the functions that were called before encountering the function containing the console.trace() line of code. Thanks to this, you can know the order in which the functions are called in the program.
For simplicity, let’s see an example:
function sum_func(x, y) {
console.trace('The sum called with ', x, 'and', y);
return x+y;
}
function calculation_func() {
return sum_func(12, 19) + sum_func(28, 22);
}
function start_func() {
var x = sum_func(15, 18);
var y = calculation_func();
}
start_func();
Output:
The sum called with 15 and 18
The sum called with 12 and 19
The sum called with 28 and 22
Method 2: Use the error object property
The following method that you can try is using the error object property.
Syntax:
function() {
// The execution code for the function
var error = new Error();
return error.stack;
}
To use this method, you can create an error object and then return the stack property. Thanks to the error object’s non-standard stack property, you’ll know the stack trace when the function was called from which line and file and with what arguments.
<!DOCTYPE html>
<html>
<body>
<h2>How To Get The Stack Trace from An Error In JavaScript</h2>
<p id="resultDiv"> </p>
<script type="text/javascript">
function stackTrace_func() {
var error = new Error();
let resultDiv = document.getElementById("resultDiv");
resultDiv.innerHTML = error.stack;
}
function children_func() {
stackTrace_func();
}
function parent_func() {
children_func();
}
parent_func();
</script>
</body>
</html>
Output:
......... ADVERTISEMENT .........
..8..
We can see the entire stack trace from the parent function to the stackTrace_func() function that caused the error has been displayed. Also, displaying line by line for every stack element will make it easier to identify errors and find solutions.
Conclusion
Thank you for reading the article; ITtutoria hope that with the methods given above, you can quickly get the stack trace from an error in JavaScript. Finally, please take a few minutes to leave your comments and suggestions below the comment section. Good luck!
Read more:
Leave a comment