4

使用 Adob​​e ColdFusion 版本 8 及更低版本,我的所有 cfqueries 都包装在一个 try catch 中,它调用 database.cfc 中名为“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:是否有一个很好的通用错误捕获器,它考虑了所有不同的异常类型(Any、Application、Database、Expression、Lock、MissingInclude、Object、Security、Template 和 SearchEngine)?

Q2:我想我也想记录这些错误,可能不是到文本文件,而是到数据库。当然,您会看到问题所在...在数据库中记录数据库错误...

Q3:如果这是本次会议的第一个错误,我可能想给某人发电子邮件。

4

2 回答 2

2

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.

于 2010-02-24T21:08:59.513 回答
1

在您的 application.cfc 中,在 OnRequestStart 函数中,您可以添加以下行:

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

通常在 act-error.cfm 上,您希望向用户显示 html,表明他们遇到了未处理的异常(使用更友好的语言)以及一些友好的链接。此外,在 act-error 上,您仍然可以访问所有变量,包括错误变量以执行您想要的操作(日志、电子邮件、更新会话变量等......)。

显然,正如您所说,如果原始错误是数据库已关闭,则登录到数据库将失败。但是,如果您不知道,ColdFusion 管理员中有异常日志,您可以启用以记录此类问题。

编辑 - 这是使用 cferror 方法的一般处理程序: http ://www.bennadel.com/blog/932-Ask-Ben-Handling-Errors-With-ColdFusion-CFError.htm

于 2010-02-23T00:05:19.893 回答