Position of CFERROR Matters

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:

<cferror template="exception.cfm" type="exception"> <cfoutput>#x#</cfoutput>

The code above will work correctly.

<cfoutput>#x#</cfoutput> <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:

<cfif not structKeyExists(application, "adminemail")> <cfset mailTo="some hard coded address"> <cfelse> <cfset mailTo = application.adminemail> </cfif>

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.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments