Ask a Jedi: Showing error details on a dev machine

This post is more than 2 years old.

Brett asks:

Hi, Do you have that "ask ray" thing going on still?

Yes, thanks for asking!

In my application.cfc, during dev / local testing, I don't want the onerror handler to work, I want the site to bomb out. I'm constantly commenting the onerror handler out in the application.cfc, is there an easier way to turn this off when testing locally?

So most likely you have some nice error handling going on where the error is completely hidden from the end user. Great. Pat yourself on the back because right there you have made your application nicer than the average! During development though it can be a bit of a pain. You actually want to see the error so you can fix it right away.

Normally what I do is a simple hostname check. If I recognize that I'm on the dev server, I'll dump the error out. So my onError may look something like this:

<cffunction name="onError" returnType="void" output="false"> <cfargument name="exception" required="true"> <cfargument name="eventname" type="string" required="true"> <cfif cgi.SERVER_NAME is "localhost"> <cfdump var="#exception#"> <cfabort> </cfif> <cfoutput>Sorry, the intern broke the internet tubes!</cfoutput><cfabort> </cffunction>

As you can see, if I recognize that I'm on my local server, I dump the exception out and abort. Otherwise I display the nice message. You can use other means to check of course. You can check the referring IP. You can check the machine name. Etc.

Now your message said that you wanted to turn the onError off. To me that implies that you want it to look a bit closer to an unhandled error. You could simply remove my dump with:

<cfthrow object="#exception#">

That simply takes the exception and throws it right back out. The end result is an error that looks like an unhandled exception.

Now - if you really do want to 'remove' onError, can you? Yes! Using the same trick Sean Corfield came up with to hide onRequest from AJAX/Flex Remoting requests:

<cffunction name="onRequestStart" returnType="boolean" output="false"> <cfargument name="thePage" type="string" required="true"> <cfif cgi.server_name is "localhost"> <cfset StructDelete(this, "onError") /> <cfset StructDelete(variables,"onError")/> </cfif> <cfreturn true> </cffunction>

Now personally I wouldn't go this route, I'd just use the example above, but it is another option you may want to consider.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

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

Archived Comments

Comment 1 by Daniel Short posted on 12/5/2008 at 1:55 AM

You can also use the built in error detail display if you'd like. Just set up a custom tag mapping to:

/install path/servers/server/cfusion-ear/cfusion-war/WEB-INF/exception

And then use this:

<cf_detail error="#Arguments.Exception.RootCause#" />

I use this in my onerror to check to see if I'm in my dev environment, and if so I display the standard CF error. This is grossly simplified (since I have a lot of other checks in there), but in the end I get my standard CF error output.

Dan

Comment 2 by Chris Bestall posted on 12/5/2008 at 2:21 AM

We just grab the IP address and compare to to a local dev range:

<cfset ip = createObject("java","java.net.InetAddress").getLocalHost()>
<cfif NOT findNoCase('192.168.1',ip.getHostAddress())>...

-Chris

Comment 3 by Scott P posted on 12/5/2008 at 5:59 AM

Same as the above comment, I wrap mine in a savecontent then either email it or if dev address then I display it.

<cfset DevList="127.0.0.1,192.168.1.1">

<cfif ListFindNoCase(allowed,cgi.remote_addr)>
<cfoutput>#mailbody#</cfoutput>
<cfelse>
<cfmail to="gbush@relocator.com" from="everyone@change.gov" subject="Error" type="html">#mailbody#</cfmail>
OTHER ERROR TEXT HERE
</cfif>

Comment 4 by kebab dylan posted on 12/5/2008 at 8:35 AM

I have been using a config file in the server root for each environment to control if cfcs get inited with each request and how to handle error debug dumps (screen in devl and email in all others).