Integrating Mura CMS and Hoth Exception Tracker for ColdFusion
Posted on June 7, 2011 at 11:56 AM in ColdFusion
A few months back Aaron Greenlee released his Hoth project, which is an exception tracking/reporting application for CFML. I've been wanting to find time (let me know if you've ever found time) to take a look at it, but haven't had a chance. Until now, that is.
After taking some time to read up on Hoth as well as how Mura handles exceptions it encounters, I set off to integrate Hoth into Mura. Now that I've done it once, I wanted to document it so that a) it can serve as a refresher course so that I can do it much more quickly the next time, and b) hopefully someone out there in the community at large can benefit from my experience with it and save themselves time as well.
I am assuming that you already have a Mura install running and powering a site with the SiteID 'mysite', and that you have already downloaded Hoth, so let's get to setting them up to play together.
Setup Hoth Mapping
Mura provides a place for you to place your custom setup code, which would ordinarily be in Application.cfc. This place is located at {Mura Install}/config/cfapplication.cfm. In my example, the full path to this file is
- /home/matt/www/websites/mura/config/cfapplication.cfm
My Hoth install is located at
- /home/matt/www/frameworks/hoth
Open up Mura's cfapplication.cfm and create the mapping to Hoth.
- <cfscript>
- this.mappings['/Hoth'] = "/home/matt/www/frameworks/hoth";
- </cfscript>
Config Hoth
Next we want to config Hoth so that it knows where to place log files, where to send emails, our application name, etc. To do this, we are going to utilize Mura's onSiteRequestStart() event handler. Open up
- {Mura Install}/mysite/includes/eventHandler.cfc
and add the following code:
- <cfscript>
- function onSiteRequestStart(
- )
- {
- var HothConfig = "";
- if ( structKeyExists(application,"HothTracker") == false )
- {
- HothConfig = new Hoth.config.HothConfig();
- HothConfig.setApplicationName(application.applicationName);
- HothConfig.setLogPath("/Hoth/logs");
- HothConfig.setEmailNewExceptions(true);
- HothConfig.setEmailNewExceptionsTo("rutroh@me.com");
- HothConfig.setEmailNewExceptionsFrom("noreply@me.com");
- HothConfig.setEmailExceptionsAsHTML(false);
- HothConfig.setHothReportURL("http://" & cgi.server_name & "/hoth-error-report/");
- application.HothConfig = HothConfig;
- application.HothTracker = new Hoth.HothTracker(application.HothConfig);
- }
- }
- </cfscript>
A couple of settings of note here:
- setEmailExceptionsAsHTML()
If you would prefer to have a dump of the entire exception emailed to you, set this to true. - setHothReportURL()
This setting defines the URL where you will view exception reports. In this example we will be creating a protected Mura page to view them (keep reading).
Setup Mura to Catch Exceptions
First thing you'll want to do is to turn off Mura's debug mode. Open up
- {Mura Install}/config/settings.ini.cfm
and set 'debuggingenabled' to false. Failure to do so will result in some exceptions being caught and others bubbling up to the view.
Create a Friendly Error Message
Next, we'll create a new Mura page to display a nice, clean message to users. We will also have it properly set a 500 response header to let the browser know that an error occurred. (This is actually a two-step process.)
Step 1: Create {Mura Install}/mysite/includes/display_objects/custom/dsp_error.cfm and add the following code:
- <cfheader statuscode="500" statustext="Internal Server Error" />
- <cfoutput>
- <h1>Error Encountered</h1>
- We apologize for the inconvenience, but an unexpected error was encountered. The webmaster has been notified.
- </cfoutput>
Step 2: Create a Mura page named 'Error' and set the body as follows:
- [mura]dspInclude('display_objects/custom/dsp_error.cfm')[/mura]
Note: Be sure to UNcheck "Include in Site Navigation" (on the "Basic" tab) and then select "Do Not Inherit Cascade" (on the "Content Objects" tab). The former is so people don't browse to your error page, while the latter keeps any problem content objects from causing an infinite loop on redirect should one of them be the cause of the exception being thrown.
Tell Mura How to Handle Exceptions
Now we are ready to tell Mura to pass any exceptions off to Hoth for tracking. We will take advantage of Mura's onSiteError() and onGlobalError() methods for this task. Open up
- {Mura Install}/mysite/includes/eventHandler.cfc
again, and paste the following code in:
- <cfscript>
- public any function onSiteError(
- required any event
- )
- {
- // track the error, then redirect to error page
- application.HothTracker.track(arguments.event.getValue("exception"));
- location(url="/error/",addtoken=false);
- }
- public any function onGlobalError(
- required any event
- )
- {
- // track the error, then redirect to error page
- application.HothTracker.track(arguments.event.getValue("exception"));
- location(url="/error/",addtoken=false);
- }
- </cfscript>
We've done two things here: First, we are passing the exception off to Hoth for logging. Second, we are redirecting the browser to our friendly error message page that we created earlier.
Note: The value of "/error/" assumes that you have Mura configured to not require the SiteID or index.cfm. If you need one or both of those for your installation, be sure to include them.
Viewing Exception Reports
We're almost done, I promise. But it kinda wouldn't make a hell of a lot of sense to trap and record exceptions if we can't ever view the exception details! For this, we're going to create another page in Mura that displays a custom display template (Step 3/4, below). But first we need the HothReportUI.cfc.
Step 1: Copy the HothReportUI.cfc from the Hoth download (/examples/ColdFusion/HothReportUI.cfc) and place it into your directory structure in a "hard-to-find" location. For my example, I renamed the file to a UUID and placed it in the following path:
- {Mura Install}/mysite/coffee/warms/the/belly/G98783C2-A76D-4F92-P067D979CA72EBD7.cfc
Step 2: Tell the ReportUI object where to find the Hoth config. On or about Line 25 of the HothReportUI file you placed into your application, replace this line
- variables.ApplicationsHothConfig = new Hoth.config.HothConfig();
with this one:
- variables.ApplicationsHothConfig = application.HothConfig;
Step 3: Create {Mura Install}/mysite/includes/display_objects/custom/dsp_hothreports.cfm. In this file, paste the code from the /views/report.html file in the Hoth download package. (You will want to strip out the <html>, <head>, and <body> tags, leaving only the styling, html markup, and JavaScript.)
IMPORTANT: You MUST update the following line to point to the location of the report CFC you setup a moment ago.
- var HOTH_REPORT_URI = '${HOTH_REPORT_URL}';
We want to provide some security for this page, so include the following at the very top of the file:
- <cfif $.currentUser().isInGroup("Admin") NEQ TRUE>
- <cflocation url="/" addtoken="false" />
- </cfif>
Here we're simply asking Mura to make sure that the user requesting the page is logged in as an Administrator before displaying the page. If not, we're sending the user to the site home page.
Step 4: Create a page named 'Hoth Error Report' in Mura that includes the custom display template we just created. Like the friendly error message page above, UNcheck "Include in Site Navigation" and select "Do Not Inherit Cascade". Then set the body content with the following:
- [mura]dspInclude('display_objects/custom/dsp_hothreports.cfm')[/mura]
Track and View
WHEW! Now you're ready to fire up Mura and have your exceptions tracked by Hoth. To give it a whirl, do something silly in a page, like <cfoutput>#</cfoutput>. You should be presented with a friendly error page while an email report is sent to you.
Happy erroring!
Latest Articles
- No recent entries.
Categories
- ColdBox (21) [RSS]
- ColdFusion (92) [RSS]
- Fusebox (3) [RSS]
- General (22) [RSS]
- jQuery (15) [RSS]
- Kalendar (1) [RSS]
- Linux (1) [RSS]
- Mura CMS (1) [RSS]
- Railo (1) [RSS]
- Rants (5) [RSS]
- Transfer (8) [RSS]
- Uni-Form Tag Library (36) [RSS]
Quick Links
Blogs I Read
Calendar
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
« Dec | ||||||
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Subscribe
Enter a valid email address.
On 8/2/11 at 8:49 PM, Michael Evangelista said:
On 8/2/11 at 9:59 PM, Matt Quackenbush said:
(Namely, we've added a check for local development environment and just dump the exception rather than wasting time by sending it to Hoth. We've also added a check to make sure that the page causing the exception is not the friendly error message page, since you end up in an endless redirect loop if it is.)
On 8/3/11 at 3:33 AM, Michael Evangelista said:
On 10/22/11 at 7:06 PM, Greg Moser said:
First and foremost, great post. I liked it so much in fact I actually just hammered out a quick Mura plugin for Hoth that can be found here (https://github.com/gregmoser/MuraHoth). Hopefully you might find it useful going forward. I'd be interested to hear any feedback you might have as well.
On 2/9/12 at 1:41 AM, Mike Schierberl said:
On 2/9/12 at 6:12 AM, Matt Quackenbush said:
Sorry I don't have a better answer for you. It has been a really long time since I've looked at this stuff, and my memory isn't the best. :-(
On 8/24/12 at 11:42 PM, Justin said:
Can this be done easily with Hoth?
On 8/29/12 at 4:42 PM, Matt Quackenbush said:
On 9/12/12 at 7:22 PM, Justin said:
On 10/31/12 at 6:43 PM, Matt Quackenbush said: