Is your template missing?

Yesterday I ran across some code that I’d forgotten about.  Coldfusion’s Application.cfc contains a number of functions that are called on every page request.  It steps through the functions like so:

OnApplicationStart()
OnSessionStart()
OnRequest() (here’s where the page actually happens)
OnRequestEnd()
OnSessionEnd()
OnApplicationEnd()

If there’s an error during the page processing, the OnError() function is called. The function that I’d forgotten about, since it’s less commonly known, is the onMissingTemplate().  This function will fire off if the page that is being navigated to turns up missing.  There’s a lot that can be done with that.  It would be one way to create a custom 404 (page not found) page.  You could forward to the page and then give some sort of explanation and a series of related links.  In the application that I am using it for, I’m just logging where the user came from so that I can find out why they’re getting there.  In our case, it’s not that our server is set up incorrectly (which sometimes is the case since we use host headers and the user types in the wrong URL), there appears to be an issue with our client’s DNS routing instead. So the idea is that I log the user’s IP address and the page and URL they’re trying to visit on the default website, and we have some information to send back to them to fix the issue.  Here’s the code for the simple logging implementation.

<cffunction name="onMissingTemplate" returnType="boolean" output="false">
<cfargument name="thePage" type="string" required="true" />     <cfif not fileExists(expandpath(Arguments.thePage))>
<cflog file="#this.name#" type="error" text="Looking for url #cgi.server_name# on port #cgi.SERVER_PORT#. Page not found: #arguments.thePage# for #cgi.REMOTE_ADDR#" >
</cfif>
<cfreturn true>

</cffunction>