Getting the call stack within window.onerror

  • Thread starter sabine.michael.ratcliffe
  • Start date
S

sabine.michael.ratcliffe

Has anybody managed to get a full stack trace from within
window.onerror? I am only able to obtain a trace back to the onerror
event itself which is pretty pointless.
 
J

Jonas Raoni Soares Silva

Has anybody managed to get a full stack trace from within
window.onerror? I am only able to obtain a trace back to the onerror
event itself which is pretty pointless.

If you use the try/catch statement, at least on Opera and Firefox,
you'll have access to the stack trace, if it's not enough, try a
debugger.
 
S

sabine.michael.ratcliffe

Yes, but in Firefox if you use a try catch and then access error.stack
the only things contained in the stack trace are the onerror handler
and the one previous action (already contained in url and line
number).

I am using this for error logging on client machines so it would be
useful to see exactly what caused the error and what was on the stack
at the time the error was triggered.
 
S

sabine.michael.ratcliffe

If you run my code as it is you get a nice call stack as follows:
Stack Trace:
doit3('ghi', 'jkl')
doit2('abc', 'def')
doit('[object MouseEvent]')
onclick('[object MouseEvent]')

If you uncomment barf (line 14) you receive the following call stack:
Stack Trace:
errorFunction('barf is not defined', 'file:///C:/Users/Ratcliffes/
De...', '14')

I receive identical results if I use the typical try ... catch(e) ...
e.stack method.

Surely there is some way to access the call stack as it was at the
time the error occurred? This would be invaluable for error logging in
complex web apps. Obviously telling every client to install Firebug
and debug it themselves is not a realistic option.

Here is my current code:
<html>
<head>
<script>
try {window.onerror = errorFunction;} catch(er) {}
function doit(event) {
doit2('abc','def');
}

function doit2(foo,bar) {
doit3('ghi','jkl');
}

function doit3(foo,bar) {
//barf();
var strValues='',
el=document.getElementById('content');

strValues += escape(stackTrace(arguments.callee)) + '\n';

if(el) {
el.value=unescape(strValues);
}
}

function errorFunction(msg,url,line) {
document.getElementById('content').value=unescape(escape
(stackTrace(arguments.callee)));
return true;
}

function stackTrace(startingPoint) {
var stackTraceMessage = 'Stack trace:\n';
var nextCaller = startingPoint;

while(nextCaller) {
stackTraceMessage += getSignature(nextCaller) + '\n';
nextCaller = nextCaller.caller;
}

stackTraceMessage += '\n';
return stackTraceMessage;
}

function getSignature(theFunction) {
var x,nextArgument,
signature = getFunctionName(theFunction)+'(';

if (theFunction.arguments) {
for(x=0; x<theFunction.arguments.length; x++) {
nextArgument = theFunction.arguments[x];

if (nextArgument==null) {
nextArgument = '?';
}

if(nextArgument.length > 30) {
nextArgument = nextArgument.substring(0, 30) +
'...';
}

signature += "'" + nextArgument + "'";

if(x < theFunction.arguments.length - 1) {
signature += ', ';
}
}
}

signature += ")";
return signature;
}

function getFunctionName(theFunction) {
var definition,name;

if(theFunction.name) {
return theFunction.name;
}

definition = theFunction.toString();
name = definition.substring(definition.indexOf('function') +
8,definition.indexOf('('));
if(name) {
return name;
}

return 'anonymous';
}
</script>
</head>
<body>
<input type="button" value="Go" onclick="doit(event);" /><br><br>
<textarea id="content" cols="80" rows="20"></textarea>
</body>
</html>
 
T

Thomas 'PointedEars' Lahn

Has anybody managed to get a full stack trace from within
window.onerror? I am only able to obtain a trace back to the onerror
event itself which is pretty pointless.

It does not work as you expect because the `onerror' event-handler
property and runtime exceptions are mutually exclusive concepts of
error handling.

Assigning a Function object reference (an event listener) to the
`onerror' property of Window host objects is a *proprietary* means in
some NN4-compatible *DOM APIs* to have the *user agent* (not: the
script engine) call the referenced function if *any* error occurs
*anywhere* from that point forward. (Assigning `null' to that
property would disable this functionality again. The property is also
available for proprietary Image host objects whereas only errors
related to loading the image are handled.)

A stack trace, on the other hand, is related to an execution context
in which runtime exceptions (handled with try...catch...finally) were
created or thrown (with `new Error()' or `throw' or by API
components). The latter are a *standardized* *language* feature (as
of the ECMAScript Language Specification, Edition 3 Final), and thus
are handled by the *script engine* instead.

So, because with `window.onerror' the caller is the user agent and not
the execution context in which you assigned to `window.onerror', your
stack trace is rather empty in the event listener.


HTH

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,123
Messages
2,570,740
Members
47,296
Latest member
EarnestSme

Latest Threads

Top