Shirzad (cool name!) asks:
There are a couple ways of handling errors with ColdFusion 8's Ajax features. Here is a quick overview of some of them in regards to the cfdiv tag. First, let's make an error. I'll take a simple cfdiv:How do you prevent the Coldfusion Ajax alert message "error retrieving markup..."? Sometimes errors are inevitable. I want to prevent users on our public site from seeing the javascript alert that pops up whenever there's an error in a coldfusion ajax container (or at the very least change the message so it isn't asking people to add cfdebug to the parameters). Is there any way to do this?
1 <cfdiv id="testdiv" bind="url:test2.cfm"/>
And here is test2.cfm:
1 <cfthrow>
As you can guess, this will immediately throw an error when loaded by the div. The cfdiv tag supports an onBindError attribute. It lets you specify a JavaScript function to run when an error occurs (big surprise there). Here is a modified version of our original script:
1 <script>
2 function handleError(c,m) {
3 console.log('error '+c+' '+m);
4 }
5 </script>
6
7 <cfdiv id="testdiv" bind="url:test2.cfm" onBindError="handleError"/>
The docs specify that the error handler should take two arguments - a status code and message. When I ran this, I correctly saw the error log, but I also saw the 'naked' CF error in the div. I thought perhaps that the onBindHandler would automatically hide the error but it doesn't. I then tried this:
2 function handleError(c,m) {
3 console.log('error '+c+' '+m);
4 }
5 </script>
6
7 <cfdiv id="testdiv" bind="url:test2.cfm" onBindError="handleError"/>
1 document.getElementById("testdiv").innerHTML = "Error!";
But for some reason, it wouldn't work. I was able to get this to work:
1 ColdFusion.navigate('/user.cfm','testdiv');
The file user.cfm was just another random file on my server. You would normally point it to a file that had some kind of 'Error' message.
Another option is to specify a global error handler. This is done with the setGlobalErrorHandler:
1 function handleGError(s) {
2 console.log('global error called '+s);
3 }
4
5 ColdFusion.setGlobalErrorHandler(handleGError);
Notice though that this function only takes one argument, a message. I don't know why - but it seems to me as if it should work the same as the error error. Anyway, if you use this, and remove the onBindError, the global error handler will fire.
As yet another example of how you can handle errors, the cfajaxproxy tag has an onError attribute. You can also set a specific error handler for an instance of an AjaxProxy object.
Check the docs for other tags and how they can provide support for this as well. (Consider this my "This your homework" part.) CFGRID for example supports onError which works the same as the onBindError for cfdiv.
2 console.log('global error called '+s);
3 }
4
5 ColdFusion.setGlobalErrorHandler(handleGError);
Comment 1 written by Michael de la Morena on 9 September 2008, at 1:42 PM
This is how the cfajax.js determines if a response is an error.
$A.isRequestError=function(req){
return ((req.status!=0&&req.status!=200)||req.getResponseHeader("server-error"));
};
Comment 2 written by Raymond Camden on 9 September 2008, at 1:46 PM
So here is an idea - use onError to handle the errors, and if the error occurs on an ajax request (you can sniff the request), you could just throw a new exception.
Comment 3 written by Henry Ho on 9 September 2008, at 1:54 PM
Comment 4 written by Paul on 9 September 2008, at 2:24 PM
Comment 5 written by Raymond Camden on 9 September 2008, at 3:02 PM
Comment 6 written by Paul on 9 September 2008, at 3:35 PM
That took me right to the line of code that was causing the problem. Excellent! Thank you!
Comment 7 written by Shirzad on 9 September 2008, at 7:32 PM
Comment 8 written by Raymond Camden on 9 September 2008, at 8:08 PM
Comment 9 written by Shirzad on 10 September 2008, at 8:53 AM
Comment 10 written by Raymond Camden on 11 September 2008, at 9:54 AM
Comment 11 written by Shirzad on 13 September 2008, at 1:09 PM
Comment 12 written by Anthony Patch on 27 January 2009, at 9:51 AM
Thanx,
Tony
Comment 13 written by Raymond Camden on 27 January 2009, at 10:43 AM
<cfgrid autowidth="true" name="entries" format="html" width="600" bind="url:getentries2.cfm?page={cfgridpage}&pagesize={cfgridpagesize}&sort={cfgridsortcolumn}&dir={cfgridsortdirection}" onError="handleError">
Comment 14 written by Anthony Patch on 27 January 2009, at 7:59 PM
That didn't work when there was an AJAX error. It just threw the typical CF javascript popup with alert recommending to turn CFDEBUG on. It didn't call the handleError function. Any ideas what may be happening?
Tony
Comment 15 written by Raymond Camden on 27 January 2009, at 8:19 PM
[Add Comment] [Subscribe to Comments]