404 Error Handling in CFWheels
03/30/2010
This is fairly simple, but I figured that I would share my approach for 404 error pages in ColdFusion on Wheels and see if anyone has a different/better way of doing it. This example demonstrates code used on cfwheels.org.
The Strategy
What I really wanted was a function that I could call whenever a given view’s record could not be found.
In my example case, I wanted to handle user IDs in the People Directory that represent records that don’t exist. I had ended up removing user 31 (and a few others), so I wanted to display a helpful 404 error message every time http://cfwheels.org/user/profile/31 was accessed.
404 Error Page
So the first step was to create the 404 page itself, which I stored at views/main/error404.cfm.
The real meat is the fact that I put the <cfheader> 404 reference in the view file. I look at anything that’s sent to the browser as a job for the view to handle, so that’s why I put the call there instead of in the controller file. In fact, because the page is fairly “dumb,” I didn’t put anything in the Main controller.
Rendering Helper
I also put a quick render404() function in the base controller at controllers/Controller.cfc so that I wouldn’t need to manually call renderPage(controller="main", action="error404") every time that I wanted to reference this new view. Your preference may be to not do this, but I’ll leave that up to you. :)
Handling 404 Errors in the Controller
The last step involved actually using this functionality in the case that an invalid record ID was passed in the URL. So the user/profile action now looks like this:
Fairly simple stuff. When loading the user object, I check to see if an object was returned. If not, then show the 404 page. Pretty reusable, and it only requires an additional if/else block in the controller to decide what to do.
Plus the file at events/onmissingtemplate.cfm can just use <cfhttp> to phone http://cfwheels.org/main/error404 in order to display the exact same error message during a more generic “template not found” scenario.
Besides identifying other places in the application to call render404(), that’s pretty much it.

Comments are Closed