良好的通用 try/catch 例程

发布于 2024-08-22 03:36:33 字数 726 浏览 5 评论 0原文

使用 Adob​​e ColdFusion 版本 8 及更低版本,我的所有 cfqueries 都包含在 try catch 中,该 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:如果这是本次会话的第一个错误,我可能想给某人发送电子邮件。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风夜微凉 2024-08-29 03:36:33

我认为您的问题通常适用于错误处理,因此您可能会在与语言无关的问题中找到一些有用的答案。

1. Soldarnal 已经回答了问题 1,我倾向于同意 - 尝试捕获可能发生错误的每个位置(例如数据库交互)需要做很多工作,最好有一个捕获所有错误并使用 try/ 的全局错误处理程序在很可能发生错误并且您希望继续执行的地方捕获(通常,如果您的站点无法访问数据库,则会有点卡住)。

2. 关于日志记录,cflog 是最强大的,因为您几乎总是可以记录到文件,除非您的服务器出现严重问题。接下来是 cfmail - 服务器应该将其无法发送的所有邮件排队,尽管还有更多可能会出错。接下来是 cfhttp,您可以将错误记录到外部站点,但发送错误时需要将其启动,否则您将丢失它。最后,您可以登录到数据库,但如果您遇到困难,请再次登录。

您可以使用组合,例如将错误记录到数据库,除非数据库不可用,然后记录到文件并在数据库备份时将错误重新插入到数据库中。

3. 如果您正在跟踪数据库中或通过外部服务等错误,您应该能够将其配置为在出现第一个错误时向您发送电子邮件,或者 cfmail 将允许您在所有错误上发送邮件。错误。我们使用自定义的错误处理脚本,但网络上可能存在一些错误处理脚本。

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.

旧话新听 2024-08-29 03:36:33

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

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

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文