Redirecting all function calls in JS -- Possible?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Redirecting all function calls in JS -- Possible?

Post by Chris Corbyn »

Hi,

I'm working on a JavaScript Exception Handling object which allows developers to log *useful* debug errors in a database via AJAX or such like (or use a user defined callback function to do as they please).

It currently works just fine if you specifically call the error handler at runtime but what I'm wondering is....

"Is there any way I can automatically capture all function calls and manipulate them to use the exception handler?"

It's a bit of a longshot and probably impossible due to security implications but it would make my whole script a lot nicer if users could do something like

Code: Select all

var exceptionHandler = new ExceptionHandler();
exceptionHandler.setCallback(foo);
exceptionHandler.autoCapture(); //Capture EVERYTHING automatically

function foo(e)
{
    alert(e);
}

function somethingBad()
{
    wonky[2] = 42; //wonky is undefined so this should get handled gracefully by the exception handler
    return wonky[2];
}

var b0rken = somethingBad();
Possible?

:)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

have you looked into a try / catch scenario?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Burrito wrote:have you looked into a try / catch scenario?
That's what's going on inside the exceptionHandler object yes.

I'll try to explain better.

To save repeating the whole try/catch scenario over and over for the developer point of view I'm writing an object (code will be revealed when happy it's ready for public use) to handle it automatically. It's not simply a try/catch system... it does have other features to such as capturing information about the client and where the error occurred etc that's why I'm writing it.

Currently it accepts a user-written callback function to process the debug output and not display anything to the client. It's using eval() to run any code given to it (I'll try and change that if I can although it seems to work just fine). A user defined callback function might be something like an AJAX request to log the error in a database on the client side but for simpicity here we'll just alert() the error directly to the browser window.

Presently you'd do something like this:

Code: Select all

<script type="text/javascript" src="exceptionHandler.js"></script>
<script type="text/javascript">
<!--

var exceptionHandler = new ExceptionHandler(); //Construct our object
exceptionHandler.setCallback(handleMyErrors); //Tell it what to use as a callback function

function handleMyErrors(errorMsg)
{
    alert(errorMsg);
}

function thisIsBroken()
{
    dog[10] = 42; //No such defined variable so bail out usually but we'll handle it here
}

// -->
</script>
<body>
<a href="javascript:exceptionHandler.run('thisIsBroken()');">Click here to fail gracefully</a>
</body>
Now as you can see it might become a little tedious for the developer to have to pass all code he/she wants to exeute through the exception handler :(

In the same way that you can captureEvents() and handle them gracefully I'm hoping that I can automatically pass any executed ajavscript through the exception handler too (sounds crazy now I read it back :?).

If I can't do that then what would be second best is to capture ALL possible events -- easy.... then find out what the event call was intended for. So in the above case I'd need to know that the event was a click and that the user was running the thisIsBroken() function with no arguments.... I'd have no clue how to go about that 8O

Thanks for reading all my gibberish :P
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

*singing the famous - Nothing Gonna Change... - tune*
n00b Saibot will be there for you if you need me,
- - - -
nothing gonna change my way of answering for you,
you gotta understand how much I love that, I do...
*more music*

:lol: :lol: :lol:

Ok, here is something I put down for you under 2 min. after "reading your gibberish" :P

Code: Select all

<script type="text/javascript"> 
var oErrorLog;

function thisIsBroken() 
{ 
  dog[10] = 42; //No such defined variable so bail out usually but we'll handle it here 
} 

function myErrorHandler(sMsg,sUrl,sLine)
{
  oErrorLog.innerHTML="<b>An error has occured.</b><hr>";
  oErrorLog.innerHTML+="<u>Error</u>: " + sMsg + "<br>";
  oErrorLog.innerHTML+="<u>Line</u>: " + sLine + "<br>";
  oErrorLog.innerHTML+="<u>URL</u>: " + sUrl + "<br>";
  event.returnValue = true;
  return false;
}

window.onerror = myErrorHandler;
window.onload = function() {
 oErrorLog = document.getElementById("ErrorLog");
}
</script> 
<body>
<a href="javascript:thisIsBroken()">Click here to fail gracefully</a> 
<div id="ErrorLog" style="font-size:11px; width:50%"></div>
</body>
tell me if that solves it... :wink:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

n00b Saibot wrote:*singing the famous - Nothing Gonna Change... - tune*
n00b Saibot will be there for you if you need me,
- - - -
nothing gonna change my way of answering for you,
you gotta understand how much I love that, I do...
*more music*

:lol: :lol: :lol:

Ok, here is something I put down for you under 2 min. after "reading your gibberish" :P

Code: Select all

<script type="text/javascript"> 
var oErrorLog;

function thisIsBroken() 
{ 
  dog[10] = 42; //No such defined variable so bail out usually but we'll handle it here 
} 

function myErrorHandler(sMsg,sUrl,sLine)
{
  oErrorLog.innerHTML="<b>An error has occured.</b><hr>";
  oErrorLog.innerHTML+="<u>Error</u>: " + sMsg + "<br>";
  oErrorLog.innerHTML+="<u>Line</u>: " + sLine + "<br>";
  oErrorLog.innerHTML+="<u>URL</u>: " + sUrl + "<br>";
  event.returnValue = true;
  return false;
}

window.onerror = myErrorHandler;
window.onload = function() {
 oErrorLog = document.getElementById("ErrorLog");
}
</script> 
<body>
<a href="javascript:thisIsBroken()">Click here to fail gracefully</a> 
<div id="ErrorLog" style="font-size:11px; width:50%"></div>
</body>
tell me if that solves it... :wink:
You're a genius! onerror.... I never new that existed. Hopefully that the only thing I need anway... I just redirect *errors* to the exception handler -- even better than using eval().

Thanks ;)
Post Reply