Trapping missing Fuseactions in Fusebox
April 03, 2008
If you want to trap and handle calls to invalid fuseactions, whether the url is mistyped or the link has expired, then you have two choices with Fusebox.
Method 1
The first is to use the inbuilt errortemplates, which is the default behaviour in Fusebox. You can then customise the genericErrorMessage.cfm template in the Fusebox5/errortemplates directory to show a nice error message for invalid fuses and circuits.
Example genericErrorMessage.cfm:
<cfoutput>
<cfswitch expression="#cfcatch.type#">
<cfcase value="fusebox.undefinedCircuit,fusebox.undefinedFuseaction">
<cfheader statuscode="404" statustext="Not Found" />
<h2>Page Not Found</h2>
<p>An Error of type "#cfcatch.type#" has occurred</p>
</cfcase>
<cfdefaultcase>
<h3>This is the template "errortemplates/#cfcatch.type#.cfm"</h3>
<h2>An Error of type "#cfcatch.type#" has occurred</h2>
<h4>#cfcatch.message#</h4>
<p>#cfcatch.detail#</p>
</cfdefaultcase>
</cfswitch>
</cfoutput>
Method 2 (My preference)
If you just want to use one set of the Fusebox5 Core Files then it is better to catch a missing fuseaction in the fusebox.init.
Example fusebox.init.
<cfif ( StructKeyExists(myFusebox.getApplication(), "fuseactionvariable") ) >
<cfset variables.fa2Check = attributes[myFusebox.getApplication().fuseactionvariable] />
<!--- check that the fuseaction is valid... --->
<cfif (ListLen(variables.fa2Check, ".") neq 2)
Or (
Not StructKeyExists(myFusebox.getApplication().circuits, ListFirst(variables.fa2Check, '.') )
)
Or (
Not StructKeyExists(myFusebox.getApplication().circuits[ListFirst(variables.fa2Check,'.')].fuseactions, ListLast(variables.fa2Check, '.') )
) >
<!---
fuseaction is invalid, could set it to the defaultFuseaction or leave it and allow
fb errortemplates to trap it...
I'm going to flag it as a 404 and show an custom page in my fusebox application
--->
<cfheader statuscode="404" statustext="Not Found" />
<cfset attributes[myFusebox.getApplication().fuseactionvariable] = "Site.NotFound" />
</cfif>
</cfif>
You will obviously need to set up a circuit called Site with a Fuse of NotFound for this to work! By doing this you can still use your layout files and therefore images, css etc.
If you're using the no XML version of Fusebox you might want to read my post Trapping missing Fuseactions in Fusebox no XML
- Posted in:
- ColdFusion
- Fusebox
7 comments
Leave a comment
If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)





Comment by Will Wilson – April 22, 2008
To set it to the default fuseaction you need to replace this:
<cfheader statuscode="404" statustext="Not Found" />
<cfset attributes[myFusebox.getApplication().fuseactionvariable] = "Site.NotFound" />
With:
<cfset attributes[myFusebox.getApplication().fuseactionvariable] = myFusebox.getApplication().defaultFuseaction />
That should do the trick!
Comment by John Whish – April 22, 2008
Comment by Will Wilson – May 06, 2008
Comment by John Whish – May 06, 2008
When using your code it works fine but since I'm using frame and when the fuseaction don't exist in that frame it will redirect to the Site.NotFound fuse inside the specific frame so my question is how would I make it to load on top.
Thanks and good work.
Comment by Yannick Morin – May 06, 2009
<script type="text/javascript">
if(top.location.href!=self.location.href)
{
top.location.href = self.location.href;
}
</script>
Comment by John Whish – May 06, 2009
This planet is full of smart people; you just have to find them.
I got myself a good one in YOU.
Comment by Yannick Morin – May 06, 2009