Blog

Bypassing ColdBox's Error Handling

Brad Wood April 10, 2015

Spread the word

Brad Wood

April 10, 2015

Spread the word


Share your thoughts

Error handling is one of those important things that many developers neglect.  Your code never errors once you're done testing it, right :) ColdFusion's detailed error messages were always a draw to the platform, but have become a bit of an embarrassment when surfing across a site that's sporting the default, ugly error page because the developers didn't bother to even implement a global error handler. 

When you use ColdBox, errors in your code are handled automatically by the platform.  There are very few features of ColdBox that kick in by default but this is one of them and we feel it's for very good reason.  ColdBox's debugging screen is not only prettier, but it displays a plethora of additional information to help you troubleshoot.  In ColdBox 4, we took it a step further by making ColdBox's default error template secure-by-default.  This means if you install ColdBox, don't change a single setting, and push it to production, errors on your site won't be advertised in juicy detail to the first hax0r that surfs across.

ColdBox's error handling is also incredibly powerful.   There's no need to litter your code with try/catches to ensure all errors are handled.  The framework will capture any error and gives you several ways of handling that error, whether it's the onError() convention in your handler, the exception handler event, or an interceptor.  Take your pick.  You can read about all these options here.

http://wiki.coldbox.org/wiki/ExceptionHandling.cfm

Perhaps surprisingly, this blog isn't about any of those options.  I wrote this up to describe how to completely BYPASS ColdBox's exception handling.  I've only heard one person ever want to do this, but they suggested that perhaps others had wondered the same thing and simply not voiced the question.  Honestly, I'm not sure why you'd want to bypass a perfectly good, extensible error handling subsystem to write another one from scratch, or just leave your site unsecured, but I suppose it's possible you just really like the colors of the default error template.

To cause your exceptions to bubble up out of the framework, simply put this bit of code in your /config/ColdBox.cfc somewhere outside of the configure() method.

function onException() {
  throw( object=interceptData.exception);
}

Yep, it's really that simple.  The magic that lets this work is the fact that your ColdBox configuration file itself gets registered as an interceptor.  This means you can create interceptor methods right there in your config file and they'll get registered.  In this case, we're listening to the onException interception point, then simply re-throwing the exception object.  This will promote the exception up to your Application.cfc's onError() method, or if that isn't defined, the global error template defined in your CF Administrator.

 

Add Your Comment

Recent Entries

BoxLang 1.0.0 Beta 27 Launched

BoxLang 1.0.0 Beta 27 Launched

We are excited to bring you Beta 27 with enhanced XML handling, new CLI app support, improved error handling, and expanded interoperability with Java and CFML runtimes. We've also added new HTTP event hooks, improved caching strategies, and a streamlined class resolution process to make your applications more performant, even in debug modes.

Luis Majano
Luis Majano
January 31, 2025
Into the Box 2025 | Plan Your Trip With Us!

Into the Box 2025 | Plan Your Trip With Us!

Are you ready to join us for Into the Box 2025 from April 30th to May 2nd in Washington, D.C.? Let’s make your trip planning as smooth as possible. Here you’ll find Airfare discounts, Hotel Deals and fun things to do to the the best out of your trip to D.C.

Maria Jose Herrera
Maria Jose Herrera
January 30, 2025