Home > CFWheels, ColdFusion, Splash CMS > onError Handling for CFWheels and Splash CMS

onError Handling for CFWheels and Splash CMS

February 4th, 2010

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!

CFWheels, ColdFusion, Splash CMS ,

  1. February 4th, 2010 at 19:51 | #1

    Be careful redirecting back to an action. If you have an controller filters and an error exists in there, you could end up with a never ending loop (ultimately ending in a hard crash).

    Just an fyi but great post.

    • Clarke Bishop
      February 5th, 2010 at 07:55 | #2

      Good point John! My approach is best for a site that’s ready for production and that has a clean, error-free, error page.

  2. George Murphy
    December 3rd, 2010 at 21:10 | #3

    How would you handle the onerror to handle 2 seperate layouts. One for the admin and one for public?

  3. George Murphy
    February 9th, 2011 at 14:54 | #4

    Hi you said to place the redirect code at the top of your events/onerror.cfm page. That has 2 methods and I get an application error when I try and place the redirect code there. Where exactly should it go? Thanks for your help.

  4. Clarke Bishop
    February 9th, 2011 at 15:44 | #5

    @George Murphy
    Sorry, but I don’t really have any ideas for you. I don’t know why you might have 2 methods in your onerror.cfm.

    What I did was in CFWheels 1.0, and there have been a couple of important updates since then.

    The best thing is to ask the CFWheels Google Group at http://cfwheels.org/community

    Good Luck!

  1. No trackbacks yet.