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.cfm file as this can be set for each individual web application.

Example fusebox.init.cfm:


<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

 


7 comments

  1. How would you suggest setting it to the defaultFuseAction? Im thinking of just using <cflocation url="#request.myself##xfa.home#" />, but it doesn't seem right

    Comment by Will Wilson – April 22, 2008
  2. Hi Will,
    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
  3. Much delayed response, but thanks for the help, the trick it did! ;)

    Comment by Will Wilson – May 06, 2008
  4. Glad I could help :)

    Comment by John Whish – May 06, 2008
  5. Hi Will,

    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
  6. Hi Yannick, if you're using a frameset then you'll need to add some javascript to your "Site.NotFound" display page which breaks out of frames. Something like this should do it:

    &lt;script type=&quot;text/javascript&quot;&gt;
    if(top.location.href!=self.location.href)
    {
    top.location.href = self.location.href;
    }
    &lt;/script&gt;

    Comment by John Whish – May 06, 2009
  7. Thank Speedy Gonzalez,

    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

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.