Good regular try / catch routine

Using Adobe ColdFusion version 8 and below, all my cfqueries are wrapped in a try catch, which 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 sign of general-purpose errors that takes into account all different types of exceptions (Any, Application, Database, Expression, Lock, MissingInclude, Object, Security, Template, and SearchEngine)?

Q2: I think I would also like to log these errors, perhaps not in a text file, but in a database. Of course, you see a problem with this ... logging database errors in the database ...

Q3: And I can send someone an email if this is the first error of this session.

+4
source share
2 answers

I think your questions relate to error handling in general, so you can find helpful answers in language agnostic questions.

1. Soldarnal answered question 1, and I would agree with pleasure - itโ€™s a lot of work to try to catch every place where an error may occur, for example, interacting with a database, and it is better to have a global error handler that catches all errors and uses try / catch to places where there is a high probability of an error occurring, and you want to continue execution (usually, if your site cannot access your database, itโ€™s a bit stuck).

2. With regard to logging, cflog is the most reliable, since you can almost always log into a file if you have no serious problems with your server. cfmail next - the server should queue any letters that it cannot send, although there it is no longer there. Further, cfhttp you can register your errors on an external site, although when sending an error it must be inserted, or you will lose it. Finally, you can enter the database, but then again, if it swallows.

You can use a combination, for example. register errors in the database if the database is unavailable, and then write to files and insert errors into the database when it is backed up.

3. If you track errors, for example. in the database or through an external service, you should be able to configure this to send email on the first error, or cfmail will allow you to send mail for all errors. We use a custom error handling script, but there may be some movement on the Internet.

+2
source

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

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

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

Obviously, as you state, logging into the database will fail if the original error was that the database was disconnected. But, if you did not know that there are error logs in the ColdFusion log, you can resolve such problems.

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

+1
source

Source: https://habr.com/ru/post/1302071/


All Articles