Open In App

How to get JavaScript stack trace when throw an exception ?

Last Updated : 25 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A stack trace is a crucial tool for identifying errors in a program when a function is called. It allows programmers to pinpoint where an error or exception occurred and understand its cause. Stack traces report the active stack frames at a specific moment during execution, showing the memory allocated dynamically in the stack. This method is commonly used in debugging to trace errors and helps programmers diagnose issues effectively.

Stack

In this data structure where data can be stored in the form of last in first out (LIFO). It means the stack is a type of storage and when a group of data comes inside the stack, it stores them one by one in such a removed manner where the last data is removed first and the first data is removed last. The way any group of data is stored the and removed from the stack can be understood by stack trace.

There are several methods by which we can get a stack trace for JavaScript when throwing an exception. which are as follows:

Using console.trace

The console object also has a method called console.trace() method, which gives you the trace on the console. Every time when it is called stack trace generate for the function. We can understand this with the help of this example.

JavaScript
// Sum function
function sum(a, b) {
    console.trace('sum called with ', a, 'and', b);
    return a + b;
}

// Calculation function 
function calc() {
    return sum(8, 11) + sum(9, 14);
}

// Start function
function start() {
    var a = sum(2, 3);
    var b = calc();
}

// Calling start function 
start();

Output:

/usr/bin/node stacktrace.js
Trace: add called with  2 and 3
    at sum (/home/dev/Documents/stacktrace.js:2:13)
    at start (/home/dev/Documents/stacktrace.js:11:13)
    at Object. (/home/dev/Documents/stacktrace.js:16:1)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
    at internal/main/run_main_module.js:17:11
Trace: add called with  8 and 11
    at sum (/home/dev/Documents/stacktrace.js:2:13)
    at calc (/home/dev/Documents/stacktrace.js:7:12)
    at start (/home/dev/Documents/stacktrace.js:12:13)
    at Object. (/home/dev/Documents/stacktrace.js:16:1)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
    at internal/main/run_main_module.js:17:11
Trace: add called with  9 and 14
    at sum (/home/dev/Documents/stacktrace.js:2:13)
    at calc (/home/dev/Documents/stacktrace.js:7:25)
    at start (/home/dev/Documents/stacktrace.js:12:13)
    at Object. (/home/dev/Documents/stacktrace.js:16:1)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
    at internal/main/run_main_module.js:17:11


Using Error Object

We can create an Error object and return stack attribute. A non-standard stack property of Error object gives you the stack trace when that particular function was called from which line and file, and with what arguments. The stack string proceeds from the most recent calls to earlier ones.

Example: To demonstrate the getting JavaScript Stack tree when throwing an exception using error object.

JavaScript
// Sum function
function sum(a, b) {
    console.log(new Error().stack);
    return a + b;
}

// Calculation function 
function calc() {
    return sum(8, 11) + sum(9, 14);
}

// Start function 
function start() {
    var a = sum(2, 3);
    var b = calc();
}

// Calling start function 
start();

Output:

/usr/bin/node trace.js
Error
    at sum (/home/dev/Documents/trace.js:2:17)
    at start (/home/dev/Documents/trace.js:11:13)
    at Object. (/home/dev/Documents/trace.js:16:1)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
    at internal/main/run_main_module.js:17:11
Error
    at sum (/home/dev/Documents/trace.js:2:17)
    at calc (/home/dev/Documents/trace.js:7:12)
    at start (/home/dev/Documents/trace.js:12:13)
    at Object. (/home/dev/Documents/trace.js:16:1)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
    at internal/main/run_main_module.js:17:11
Error
    at sum (/home/dev/Documents/trace.js:2:17)
    at calc (/home/dev/Documents/trace.js:7:25)
    at start (/home/dev/Documents/trace.js:12:13)
    at Object. (/home/dev/Documents/trace.js:16:1)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
    at internal/main/run_main_module.js:17:11

Using caller object

We implemented a stacktrace function that returns the call history as a string up to the point it was called. It uses a recursive st2 function, called with arguments.callee.caller, to traverse the call-tree and build the stacktrace string.

Example: To demonstrate the getting JavaScript Stack tree when throwing an exception using caller object.

JavaScript
// Sum function 
function sum(a,b) { 

    // Calling stacktrace function 
    console.log(stacktrace()); 
    return a+b; 
} 

// Calculation function 
function calc() { 
    return sum(8, 11) + sum(9, 14); 
} 

// Start function 
function start() { 
    var a = sum(2, 3); 
    var b = calc(); 
} 
    
// Calling start function 
start(); 

// Stacktrace function 
function stacktrace() { 
function st2(f) { 
    var args = []; 
    if (f) { 
        for (var i = 0; i < f.arguments.length; i++) { 
            args.push(f.arguments[i]); 
        } 
        var function_name = f.toString(). 
        split('(')[0].substring(9); 
        return st2(f.caller) + function_name + 
        '(' + args.join(', ') + ')' + "\n"; 
    } else { 
        return ""; 
    } 
} 
return st2(arguments.callee.caller); 
} 

Output:

/usr/bin/node stackt.js
([object Object], function require(path) {
      return mod.require(path);
    }, [object Object], /home/dev/Documents/stackt.js, /home/dev/Documents)
start()
sum(2, 3)
([object Object], function require(path) {
      return mod.require(path);
    }, [object Object], /home/dev/Documents/stackt.js, /home/dev/Documents)
start()
calc()
sum(8, 11)
([object Object], function require(path) {
      return mod.require(path);
    }, [object Object], /home/dev/Documents/stackt.js, /home/dev/Documents)
start()
calc()
sum(9, 14)

Next Article

Similar Reads