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  |  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 »
7,572 Comments

@Joey,

That is a funky difference!


Mar 1, 2010 at 3:54 PM // reply »
4 Comments

I am using the OnError function in application.cfc and I noticed a strange occurance. If I mistype a cf tag (ie. <cfssset x = y />) the OnError does not catch it, just a generic CF error is displayed. Furthermore even if I use a cftry cfcatch around that code, with type = "any", CF still displays its own error and will not allow me to catch it.

Not that I plan on purposely adding bad code, but it would be nice if I could catch it just in case. Do you have any idea if this is a known bug?


Mar 1, 2010 at 4:06 PM // reply »
4 Comments

I forgot to mention that my mistyped tag was located in my index.cfm file.

Interestingly enough I created the same error in a cfc which I called as an object. In that way the error was caught and displayed the way I wanted to from the OnError function.

Am I missing something?


Mar 1, 2010 at 4:27 PM // reply »
7,572 Comments

@Jason,

It's possible that parsing errors are not handled in the same way; theoretically, a site would never go live *with* parsing errors.

It's also possible you have an error in your onError() method handler; if you do, the original error will be allowed to bubble up to the top (without the error in the onError() event handler being displayed).


Mar 1, 2010 at 4:42 PM // reply »
4 Comments

@Ben

Thank you for responding so quickly.

I thought I would post my code. I was just doing a simple POC when I noticed this.

application.cfc:
----------------------------------------
<cffunction name="OnRequest" access="public" returntype="void" output="true">

<cfargument name="TargetPage" type="string" required="true" />

<cfinclude template="#Arguments.TargetPage#" />

<cfreturn />
</cffunction>

<cffunction name="OnError" access="public" returntype="void" output="true">

<cfargument name="pass_cfcatch" required="Yes" type="any" />

<cfdump var="#pass_cfcatch#" />
</cffunction>
----------------------------------------

The file that the OnError would not catch was onerror_parsing.cfm with just <cfssset x = y /> in it.

The other file that OnError DID catch was onerror_parsing_cfc.cfm that called a cfc with <cfset Variables.onerror_template = createObject("component", "onerror_parsing").init() />. In the onerror_parsing.cfc file I had the same <cfssset x = y /> in it.

I only noticed this cause I was trying to see how the OnError would react to different types of errors. I agree with you and hope that any error like this would never reach the live server (especially since I have a Dev AND Staging server), I just thought it was worth noting.

Thanks again.


Mar 1, 2010 at 4:55 PM // reply »
4 Comments

Okay, sorry about flooding your comments, but on a whim I tried one more thing. I made no changes to the application.cfc file I posted above, but this time I created another .cfm file that simply had <cfinclude template="onerror_parsing.cfm" /> in it.

When loading it this way the OnError in the application.cfc caught and dumped the error.

Of course, I think the moral of this story is don't let these kind of errors make it to your live server, but again, I thought it was worth noting.

On a side note, I did work for a company that had a programmer (who was eventually denied access to the live server) who kept insisting on making changes directly on the live server - he would occasionally make these kind of errors. Not fun.


Mar 1, 2010 at 6:10 PM // reply »
7,572 Comments

@Jason,

Hmm, odd that it would catch one parsing error and not the other. There's probably something subtle and tricky going on :)


Mar 19, 2010 at 4:47 PM // reply »
5 Comments

@Jason and @Ben,
I've been doing some CF9 refactoring on our systems and noticed an odd occurrence with onError as well.

Found a way to work around my problem, but what I saw was...

Background:
Our platform is OO and creates a CFC (our API) into the session scope onSessionStart. That API is then referenced in each CFC's variables scope so it's easily accessed from any function.

If an error was generated on the same request as the one that ran onSessionStart, the onError function call had no access to the alias in the variables scope. As if it was never put there. But it IS there as that happens in the constructor of the base-class that each CFC extends.

So a call of
<cfset id = API.getUserID()> fails in the error handler, but
<cfset id = session.API.getUserID()> works.

Again, if I cause the error in a request AFTER the session is created, everything works. But if the session is instantiated in that request I get a standard CF error saying it can't find API.

I wish I could say I understand what's happening so-as to shed light on this topic, but maybe my experience could help somehow.

For clarification, the CFC being called to generate the error is not directly called, it's being processed by the API, so the API MUST be fully initialized and loaded into the variables scope to even execute the error function.

-Bry


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 21, 2010 at 11:13 AM
A New Wrist Pain
@chiropractor suwanee, Spoken like someone trying to sell something. Other than for minor, temporary relief from some back pain, chiropractic treatment is nothing but placebo effect and quackery. ... read »
Mar 21, 2010 at 6:32 AM
ColdFusion CFPOP - My First Look
Apologies... The field name in the db for C. is "BounceCode" It stores the code / message which is returned in the email. Sorry for the confusion. ... read »
Mar 21, 2010 at 6:29 AM
ColdFusion CFPOP - My First Look
@Jose Galdamez, Hi Ben and Jose 1st of all.. big thanks to Jose for his Skype chat a few weeks back. Your time was much appreciated. I have come up with a rather unelegant solution to my problem a ... read »
Mar 21, 2010 at 3:42 AM
A New Wrist Pain
Chiropractic treatment is one of the best methods for treating numerous health problems naturally. After years of experience being a chiropractor, I have found that it is a powerful way to solve many ... read »
Mar 20, 2010 at 12:07 PM
Drawing On The iPhone Canvas With jQuery And ColdFusion
Simply awesome. Saved my day. ... read »
Mar 20, 2010 at 9:00 AM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
I would like to say thx for an easy way to create a bottom bar. I do have a ?. Is it possible to center the bar if i want to resize it to ex 85%. Regards Offenbach ... read »
Mar 19, 2010 at 7:26 PM
MySQL 3/4 - com.mysql.jdbc.Driver And allowMultiQueries=true
Thank you very much for this post. Adding allowMultiQueries="true" in context.xml didn't help until I added it to url as allowMultiQueries=true Good idea is to use prepared statements and it will he ... read »
Jim
Mar 19, 2010 at 4:49 PM
Nobody Puts Baby In The Corner!
Wow. This is like suddenly finding a support group for your secret shame. I'm not alone! I always liked this movie, even though it is extremely cheesy. I just wish Jennifer Grey hadn't gotten the ... read »