Application.cfc OnRequest() Method Affects OnError() Arguments

Posted July 23, 2007 at 9:22 AM

Tags: ColdFusion

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):


 
 
 

 
ColdFusion Application.cfc OnError() Arguments With OnRequest() Presence  
 
 
 

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:


 
 
 

 
ColdFusion Application.cfc OnError() Arguments Without OnRequest() Presence  
 
 
 

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




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Mar 23, 2009 at 6:45 PM // reply »
3 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!


Mar 23, 2009 at 6:47 PM // reply »
6,516 Comments

@Joey,

That is a funky difference!


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »