Ok, so I love Application Events and the new Application.cfc. I really, really love it. That being said, there are a few "issues" you should be aware of when using it:
onError: When using the onError method, every single <cflocation> call will fire the event. Kind of a silly bug, but it didn't get found till too late. Christian Cantrell came up with a nice work around to place inside your onError:
<cfreturn/>
</cfif>
If you don't have this, your error handling will fire when you don't really want it to.
onRequest: This is covered in the docs - but I know people are going to miss it. If you use onRequest, you cannot use Flash Remoting or Web Services. This is kinda silly too and I hope it gets fixed soon. I get the reasons behind it, but it should still be corrected I think. Sean came up with a nice workaround for it. If your CFCs are in a subfolder, add a Application.cfc in and use code like so:
<cfset structDelete(variables,"onRequest")/>
<cfset structDelete(this,"onRequest")/>
</cfcomponent>
Comment 1 written by Tim on 25 May 2005, at 12:27 PM
Any ideas?
Comment 2 written by Raymond Camden on 25 May 2005, at 2:41 PM
So,
cfif structkeyexists(arguments.exception,"rootCause") and .....
Comment 3 written by Tim on 25 May 2005, at 3:02 PM
Comment 4 written by Raymond Camden on 25 May 2005, at 3:05 PM
Anyway - why not simply cfdump the exception and see if it is obvious what the exception is.
Comment 5 written by Tim on 25 May 2005, at 4:08 PM
I am running a full version of MX7 with the second hotfix.
I added a CFDUMP of the exception structure in my onError tag. Turns out in your blog you say to use rootCause, but that doesnt exist. What does exist is "Type". So the example code in your entry should be this:
<cfif StructKeyExists(arguments.exception,"type") and arguments.exception.type eq "coldfusion.runtime.AbortException">
<cfreturn/>
</cfif>
Thanks for helping me lead to the right answer.
Comment 6 written by Raymond Camden on 25 May 2005, at 4:48 PM
Comment 7 written by John on 25 May 2005, at 7:31 PM
When I try the code you just posted I receive this error:
Element EXCEPTION is undefined in ARGUMENTS
I assume you are not receiving this error message, so I wonder what I could be doing wrong.
Comment 8 written by Tim on 26 May 2005, at 12:36 AM
<cffunction name="onError">
<cfargument name="Exception" required=true/>
<cfargument type="String" name="EventName" required=true/>
<cfif StructKeyExists(arguments.exception,"type") and arguments.exception.type eq "coldfusion.runtime.AbortException">
<cfreturn/>
</cfif>
... code to display error message to user...
</cffunction>
Comment 9 written by Justice on 24 October 2005, at 6:47 AM
Quick question: Does anyone else here include template-style code in the onRequest block? I have a cfinclude of all header style code right before my cfinclude of #argument.thePage#.
Comment 10 written by Raymond Camden on 24 October 2005, at 7:59 AM
I do NOT do layout in onRequest.
Comment 11 written by Justice on 24 October 2005, at 8:04 AM
Is there a reason you do not do any other code in onRequest? ie, I have an include that just has my CSS links, and 2 javascript references in it. What would be the difference in putting this in an include on each page? I used to do this in my application.cfm file. Where should it go when using application.cfc?
Comment 12 written by Raymond Camden on 24 October 2005, at 8:06 AM
I rarely use onRequest since it impacts web services and flash remoting. That being said, I do layout in a custom tag. If you did it in onRequest, it would mean EVERY page would you use the layout, which would be a problem if your site had popups, or if you wanted something different before a person was logged in.
Comment 13 written by Derek on 16 April 2006, at 11:50 PM
So how do I remove this w/o errors?
<cffunction name="onRequest" returnType="void">
<cfargument name="thePage" type="string" required="true">
<cfinclude template="#arguments.thePage#">
</cffunction>
Comment 14 written by Raymond Camden on 17 April 2006, at 8:08 AM
Comment 15 written by Derek on 17 April 2006, at 9:06 AM
Comment 16 written by Raymond Camden on 17 April 2006, at 9:20 AM
Comment 17 written by Derek on 17 April 2006, at 9:37 AM
<cffunction name="onRequestStart" returnType="boolean" output="false">
<cfargument name="thePage" type="string" required="true">
<cfscript>
if(NOT IsDefined("Application.Init") OR IsDefined("URL.ReInit")){
// init the bookiecharts cfc
Temp = createObject("component","bookiecharts.cfcs.settings").InitApplication();
//Set Global Init app flag var to true
Application.Init = true;
}
</cfscript>
<!--- Call the FormURL2Attributes tag --->
<cfmodule template="/#Application.Settings.RootMapping#/customtags/FormURL2Attributes.cfm">
<cfinclude template="/#Application.Settings.RootMapping#/udfs/udfs.cfm">
<cfsetting showdebugoutput="yes">
<cfreturn true>
</cffunction>
Comment 18 written by Raymond Camden on 17 April 2006, at 9:50 AM
THe problem you have is that onRequestStart is using it's OWN variables scope now. So your page doesn't have access to the UDFs. What you need to do, or what you CAN do I should say, is consider simply copying the UDFs to the request scope. Then you change any call like so, result = someUDF(), to: result = request.udfs.someUDF().
Comment 19 written by Derek on 17 April 2006, at 10:09 AM
Comment 20 written by Ben Nadel on 21 April 2006, at 8:02 AM
Comment 21 written by Stefan on 29 December 2006, at 5:37 PM
Happy new year,
Comment 22 written by Raymond Camden on 29 December 2006, at 5:47 PM
Comment 23 written by Stefan on 29 December 2006, at 7:40 PM
Actually, I can not get a onError triggered by cflocation at all. I tried:
<cffunction name="onError" returnType="void">
<cfargument name="Exception" required=true/>
<cfargument name="EventName" type="String" required=true/>
<cflog text="onError triggered" file="RedirLog">
</cffunction>
But it does not react at all on my cflocations. Any tip of how I should detect cflocation?
Comment 24 written by Raymond Camden on 30 December 2006, at 1:21 PM
Comment 25 written by sinuy on 8 May 2007, at 4:01 AM
i previously use cferror on Application.cfc/cfm. i found tat the '#ERROR.Diagnostics#' able to tells exactly which line of my code occurred.
does onError function has this feature as well? and how?
Comment 26 written by Raymond Camden on 8 May 2007, at 7:11 AM
Comment 27 written by Rony Fayyad on 4 June 2007, at 10:41 PM
I have some code in the onrequeststart() function which checks to see if a user is logged in, in order to access certain admin pages.
Now if they are not logged in, it would move them to the login page.
Here is a snippet of the code found in the onrequeststart function.
<cfheader statuscode="301" statustext="Moved Permanently">
<cfheader name="Admin login" value="localhost/admin/index.cfm">
<cfabort>
However, the move never happens.
It goes to the onerror() ( i added the code from this blog) and all it does now is show an empty page.
Any help here guys?
Comment 28 written by Raymond Camden on 5 June 2007, at 8:30 AM
<cfif arguments.exception.rootcause.type is "coldfusion.runtime.AbortException">
<cfreturn>
</cfif>
Comment 29 written by Jim on 16 July 2007, at 10:42 AM
Just wanted to pass along the combination of the two methods used in this thread for trapping the cfabort/cflocation on error bug. netheir one of the methods worked completely for me but when I combined both all is well.
<cfif isDefined("arguments.exception.Type") and (arguments.exception.Type eq "coldfusion.runtime.AbortException")>
<cfreturn/>
<cfelseif isDefined("arguments.exception.Type") and (arguments.exception.Type eq "Expression")>
<cfif isDefined("arguments.exception.RootCause.Type") and arguments.exception.RootCause.Type eq "coldfusion.runtime.AbortException">
<cfreturn/>
</cfif>
</cfif>
Comment 30 written by Raymond Camden on 16 July 2007, at 10:47 AM
Comment 31 written by Craig Benner on 11 March 2009, at 11:36 AM
It basically shows how the code needs to invoke the method requested when ajax/webservices are being called.
Comment 32 written by JP on 17 July 2009, at 4:08 PM
Comment 33 written by Jody Fitzpatrick on 27 November 2009, at 7:55 AM
Comment 34 written by Raymond Camden on 27 November 2009, at 8:00 AM
onRequest blocking flashremoting/CFC calls was fixed in CF9.
[Add Comment] [Subscribe to Comments]