How to Save Errors in the log
"Error Trapping Issues"
The main Error Trapping issue in ColdFusion is the fact that when an error happens,
the error page can not run any ColdFusion queries or run any CFINCLUDE routines, to say the least.
It is also only able to display its Error variables once within the page, so it
actually runs like a static page. It mainly acts as an error display page for
the user to know an error has occurred.
When you want to actually record an error to a log of some sort, your BEST option is to use JavaScript to
save the #Error.Diagnostics# variable to another page that WILL run ColdFusion with a query
or file save routine. We do this in our program by saving the error to a page within an
IFRAME which is a 1x1 pixel hidden frame on the error page. This is the only recommended way
to really do it, and we have done it effectively.
"How CF Trips Errors"
When any CF Error occurs, you want this to be caught right away, so you set the
error control in the APPLICATION.CFM page in your site. You assign the error page
by utilizing the CFERROR tag as below:
<CFERROR template="Error.cfm" type="REQUEST">
Note: This should catch all errors, but some versions of MSIE interact
with the server in a way that only shows a 500 Error instead of the Error page. This
may be due to certain types of errors, such as a CFQUERY when a table or column is not
found. All others have been documented to work correctly so far.
"How to Trap Errors"
The error trapping routine below is the best way we have figured out how to actually save
ALL of the information from the Error.Diagnostics and send it to to the ErrorSave.cfm file.
One of the issues we encountered in trying to save Error.Diagnostics to a hidden text field
was the spaces, the quotation marks, and single quotes. We output the Error.Diagnostics to
a textarea field, which then can be accessed by JavaScript to parse out the unneeded quotation marks.
Single quotes are converted to URL Encoded Format on the server side.
<CFOUTPUT>
<DIV ID=Diag style=display:none>#Error.Diagnostics#</DIV>
</CFOUTPUT>
<iframe frameborder="0" name="error" id="error" height="1" width="1" src="/blank.htm"></iframe>
<CFOUTPUT>
<form name=myForm action="/ErrorSave.cfm" target=error method=post>
<input type=hidden name=Browser value="#Error.Browser#">
<input type=hidden name=ErrorDiagnostics value="">
</form></CFOUTPUT>
<script lanugage=javascript>
function replaceIT(entry) {
out = '"'; // replace this
add = ""; // with this
temp = "" + entry; // temporary holder
while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add +
temp.substring((pos + out.length), temp.length));
}
document.myForm.ErrorDiagnostics.value = temp;
}
replaceIT(document.getElementById('Diag').innerHTML)
document.myForm.submit()
</script>
*The javascript replace function is a free script from javascriptsource.com.
"How to Save Errors in the log"
This error save routine can be done using CFFILE or CFQUERY to save the error to
a file or a database table. In our shopping cart, we save the error & browser information to the
database along with their unique user, remote host address, and date & time. We link this
to the users' stats and can see if they had additional problems and/or left the site soon after
the error occurred. This error control routine helps in preventing big issues from going
unnoticed for days before a customer calls in about it.
This wraps up this section for ColdFusion Error Handling. If you need any assistance in
working on your own custom script, we are open for business.