onError Handling for CFWheels and Splash CMS
For most websites it’s usually a good idea to have error pages that have user-friendly error messages that are styled to look like the rest of the site. Also, you may want to send yourself an eMail when an error happens. Read on to see how to make this work.
For the DonorUp website, I used CFWheels and the Splash CMS. Once I knew the tricks, it turned out to be really easy to make this work. So, I decided to write a post to explain what you need to know.
Typically, in ColdFusion, you add an onError() method to your Application.cfc file. Wheels gives you various hooks into Application.cfc, but Wheels already has an onError() method. So, you can’t add another one! Fortunately, Wheels has an easy way to handle most errors.
How to send error messages from CFWheels
All you have to do to get Wheels to send error message emails is add the following into your /config/settings.cfm file:
<cfset set(sendEmailOnError = true)>
<cfset set(errorEmailAddress = “you@yourServer.com”)>
Now, whenever there’s an error, you’ll get a really nice email with all the troubleshooting info you’ll need. Easy!
How to setup a custom error page in CFWheels or Splash
As part of its built-in error handling, Wheels runs the file at /events/onerror.cfm. Just put a redirect to your error page at the top of onerror.cfm and you’re done:
<cfset redirectTo(route=”viewer”, slug=”error-page”)>
In my case, I went into the Splash CMS admin and added a page called Error Page. This way, the page is automatically styled to match the rest of the site. The error page apologizes to the user for the error, and let’s them know I am going to fix it!
When Wheels does its error handling, it will run /events/onerror.cfm, and the redirectTo() will send them to my error page! If you aren’t running Splash, you could just use the other arguments of redirectTo() to send them to any controller and action.
You can also put your own HTML or CFML code in onerror.cfm, but I like doing it this way better as it uses all the layouts and styling of my site!
Please leave me a comment if you like this approach or have other ideas!
