Ask a Jedi: Showing error details on a dev machine

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