Twitter: raymondcamden

Address: Lafayette, LA, USA

Position of CFERROR Matters

11-30-2006 3,039 views ColdFusion 2 Comments

Chalk this up to the "I can't believe you never saw this before" file. While this is incredibly obvious, I never really ran into this today. Where you put your CFERROR tag (for those using Application.cfm instead of Application.cfc) matters. Consider:

view plain print about
1<cferror template="exception.cfm" type="exception">

The code above will work correctly.

view plain print about
2<cferror template="exception.cfm" type="exception">

The code above will not work correctly because the error occurs before ColdFusion has been told what to do with an error. As I said - incredibly obvious but it never really bit me in the rear till today.

Now you might say - why not just move it to the top of your Application.cfm file. Well that would work - but what if your exception handler itself uses variables in the Application scope? For example - an application.adminemail setting to know where to fire off error reports. I think you might consider doing code something like so in your exception handler:

view plain print about
1<cfif not structKeyExists(application, "adminemail")>
2 <cfset mailTo="some hard coded address">
4 <cfset mailTo = application.adminemail>

While it is never nice to hard code values - it might be acceptable as a last case resort. Someone remind me tomorrow and I'll post my "typical" exception handler.


  • Jason Troy #
    Commented on 11-30-2006 at 9:44 PM
    *grins* I can relate.
    This bit me after I had my datasource fail to connect, which was a a call to the "settings" table that stores the global variables for the site.
    When asked, I promptly pointed to the network engineer and said the database connection failed. (just kidding)
  • Commented on 12-01-2006 at 3:52 AM
    >> Someone remind me tomorrow and I'll post my "typical" exception handler.

    Well -- unless you're in Hawaii, or some other pacific island -- it should be tomorrow now.

    Consider yourself reminded. :)

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty