Application.cfc OnRequest() Method Affects OnError() Arguments
Posted July 23, 2007 at 9:22 AM
The other week, Thomas Messier pointed out to me that my ColdFusion Application.cfc Tutorial was a bit incomplete in the fact that it did not address the changes in the OnError() event method arguments that depend on the existence of the OnRequest() event method. To be totally honest, I did not know anything about this. I pretty much always use the OnRequest() event method, so I have never had to deal with errors that occur outside of it.
I set up a quick, little test application to see what was actually going on. In the Application.cfc ColdFusion component below, my OnRequestStart() checks for a Delete flag in the URL. If it exists, it simply deletes the OnRequest() method. Then, I throw an error in my index.cfm page and compare the two different sets of arguments.
My ColdFusion Application.cfc:
Launch code in new window » Download code as text file »
- <cfcomponent
- output="false"
- hint="Handles application level evnts.">
-
- <!--- Set up application. --->
- <cfset THIS.Name = "ErrorTest" />
- <cfset THIS.ApplicationTimeout = CreateTimeSpan( 0, 0, 0, 5 ) />
- <cfset THIS.SessionManagement = false />
-
- <!--- Set up page request. --->
- <cfsetting
- showdebugoutput="false"
- />
-
-
- <cffunction
- name="OnRequestStart"
- access="public"
- returntype="boolean"
- output="false"
- hint="Pre-page processing for each page request.">
-
- <!--- Define arguments. --->
- <cfargument
- name="TargetPage"
- type="string"
- required="true"
- hint="The template being requested."
- />
-
- <!---
- Check to see if we want to keep the OnRequest()
- method or delete it from the app component.
- --->
- <cfif StructKeyExists( URL, "delete" )>
-
- <!--- Delete the OnRequest() method. --->
- <cfset StructDelete( THIS, "OnRequest" ) />
-
- </cfif>
-
- <!--- Return out. --->
- <cfreturn true />
- </cffunction>
-
-
- <cffunction
- name="OnRequest"
- access="public"
- returntype="void"
- output="true"
- hint="Processes the requested template.">
-
- <!--- Define arguments. --->
- <cfargument
- name="TargetPage"
- type="string"
- required="true"
- hint="The template being requested."
- />
-
- <!--- Include the requested template. --->
- <cfinclude template="#ARGUMENTS.TargetPage#" />
-
- <!--- Return out. --->
- <cfreturn />
- </cffunction>
-
-
- <cffunction
- name="OnError"
- access="public"
- returntype="void"
- output="true"
- hint="Fires when an exception occures that is not caught by a try/catch block">
-
- <!--- Define arguments. --->
- <cfargument
- name="Exception"
- type="any"
- required="true"
- />
-
- <cfargument
- name="EventName"
- type="string"
- required="false"
- default=""
- />
-
-
- <!---
- Dump out the ARGUMENTS scope. Here, we want to see
- how the argument change depending on whether or not
- we have the OnRequest() method.
- --->
- <cfif StructKeyExists( THIS, "OnRequest" )>
-
- <!--- Use label with onrequest. --->
- <cfdump
- var="#ARGUMENTS#"
- label="OnError() - WITH OnRequest()"
- />
-
- <cfelse>
-
- <!--- Use label withOUT onrequest. --->
- <cfdump
- var="#ARGUMENTS#"
- label="OnError() - WITHOUT OnRequest()"
- />
-
- </cfif>
-
- <!--- Return out. --->
- <cfreturn />
- </cffunction>
-
- </cfcomponent>
And then, my simple index.cfm:
Launch code in new window » Download code as text file »
- <!--- Force an error. --->
- <cfset x = y />
Here, I am simply referring to a variable, Y, that does not exist.
When I run the page without any query string flag, the Application.cfc runs with the OnRequest() method and produces this OnError() arguments CFDump (I have collapsed some of the items for better display):
| | | | ||
| | ![]() | | ||
| | | |
Now, let's compare that to what happens when I call the same page, but this time, I send the "?delete" flag that will cause the OnRequestStart() method to delete the OnRequest() method from the Application.cfc. This time, my CFDump output is much smaller:
| | | | ||
| | ![]() | | ||
| | | |
The difference here, as Thomas Messier pointed out, is that the error generated without the OnRequest() method's presence does NOT have the RootCause structure. I don't know why this is, but this is certainly good to know.
Download Code Snippet ZIP File
Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Newer Post
Project HUGE: Why I Can't Stop Thinking About Lydia Cheng's Daughter
Older Post
Comparing ColdFusion Number Randomization Algorithms
Reader Comments
not sure what the difference is, but i will have to investigate a little. The application.cfc i am using does not have an OnRequest() and the ARGUMENTS.EXCEPTION.RootCause struct _is_ there, however if i extend that cfc and put my own OnError handler in the cfc that is extending the main one the ARGUMENTS.EXCEPTION.RootCause struct _is not_ available.
Very odd, this is definitely something that needs to be cleared up!
@Joey,
That is a funky difference!






