Over a million developers have joined DZone.

Adding A Stack Trace To JavaScript Errors

·
Basic example of adding a stack trace to JavaScript errors.


function trace(e, fn) { e.stackTrace = e.stackTrace || []; e.stackTrace.push(fn); return e; }

function d() { try { throw new Error("oh noes!"); } catch (e) { throw trace(e, "d"); } }
function c() { try { d(); } catch (e) { throw trace(e, "c"); } }
function b() { try { c(); } catch (e) { throw trace(e, "b"); } }
function a() { try { b(); } catch (e) { throw trace(e, "a"); } }

try
{
  a();
}
catch (e)
{
  alert("Error: " + e.message + "\r\n" + "Stack Trace: " + e.stackTrace.join("\r\nat "));
}
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}