tags:

views:

145

answers:

2

Using Adobe ColdFusion version 8 and below, all my cfqueries are wrapped in a try catch that calls a function in database.cfc called "CatchError".

<cftry>
   <cfquery datasource="myDatasource">
   UPDATE TableName SET
   ...
   WHERE ID = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.ID#">
   </cfquery>
   <cfcatch>
      <cfset local.result = Variables.objDatabase.CatchError(cfcatch)>
   </cfcatch>
</cftry>

Q1: Is there a good general purpose error catcher that's been written that takes into account all the different exception types (Any, Application, Database, Expression, Lock, MissingInclude, Object, Security, Template and SearchEngine)?

Q2: I'm thinking I would like to log these errors as well, maybe not to a text file but to a database. Of course, you see the problem with that...logging database errors in the database...

Q3: And I might want to email someone if it's this session's first error.

+1  A: 

In your application.cfc, in the OnRequestStart function, you can add this line:

<cferror type="EXCEPTION" exception="any" template="act-error.cfm">

Typically on act-error.cfm you want to show the user html that indicates they have encountered an unhandled exception (in more friendly language) along with some friendly links. Also, on act-error you can still access all the variables, including the error variable to do what you want (log, email, update a session variable etc...).

Obviously, as you state, logging to a database will fail if the original error was that the database was down. But, in case you were unaware, there are exception logs in the ColdFusion admin you can enable to log problems like that.

Edit - Here is a general handler using the cferror approach: http://www.bennadel.com/blog/932-Ask-Ben-Handling-Errors-With-ColdFusion-CFError.htm

Soldarnal
If you're using application.cfc, you should be using onError (http://livedocs.adobe.com/coldfusion/8/htmldocs/AppEvents_06.html#1188543), not cferror. Otherwise, that's like using a screwdriver to pound in a nail -- it might work, but you're using your tools incorrectly.
Eric Kolb
I'm curious why OnError is better? I prefer not to use it because I don't like having html output in my application.cfc.
Soldarnal
It's not "better", per se, but it is the error handling mechanism that is associated with the application.cfc framework. Consider it an exercise in occam's razor - the simplest explanations are usually the best. If you hear hooves, you'll think "horse" and not "zebra". If another developer sees application.cfc, they'll think "onError", not "cferror". The onError method does also have the advantage of being truly ubiquitous; an error that occurs before a cferror call would be unhandled. By the by, you can just as easily use a cfinclude from within the body of the onError handler if you prefer.
Eric Kolb
+1  A: 

I think your questions apply to error handling in general, so you may find some useful answers in language-agnostic questions.

1. Soldarnal has answered question 1 and I would tend to agree - it's a lot of work to try catch every location where an error may occur such as database interaction, and better to have a global error handler which catches all errors and use try/catches in places where there is a highish likelyhood of an error occurring and you want execution to continue (generally if your site can't access your database it's a bit stuck).

2. Regarding logging, cflog is the most robust as you can almost always log to a file unless there's severe problems with your server. cfmail is next - the server should queue any mails it can't send though there's more to go wrong. Next is cfhttp you can log your errors to an external site, though it will need to be up when the error is sent or you'll lose it. Finally you can log to a database, but again if that's down your stuck.

You could use a combination e.g. log errors to a database, unless the database is unavailable then log to files and reinsert the errors into the database when it's back up.

3. If you're tracking errors e.g. in a database or via an external service, you should be able to configure this to send you emails on the first error or cfmail will allow you to send mail on all errors. We use a custom built error handling script, but there may be some floating around the web.

Loftx