Ben Thomas
Ben Thomas

Reputation: 87

Coldfusion error handling

I am trying to create some custom error pages that email our developers that there has been an 404 or 500 error in coldfusion. Also it would be helpful if within the email it gives the error variables.

I have set up a error page in coldfusion and have pointed IIS(7) at it.

Also in my application.cfm file I am declaring the following:

<cferror type="exception" template="#name#">
<cferror type="request" template="#name#"> 
<cferror type="validation" template="#name#"> 
<cferror type="monitor" template="#name#"> 

My problem is that within the error file I cannot include any CF calls, CF appears to stripe them out.

Am I doing something wrong?

Thanks in advance!

Upvotes: 1

Views: 5174

Answers (3)

coldfusiondevshop
coldfusiondevshop

Reputation: 683

using a template is best precise for this reason for example... dbc.cfm. write mail sending code in dbc.cfm.

<cftry>
        <cfquery name="GetData" datasource="#Application.ds#" dbtype="ODBC" username="#Application.UserName#" password="#Application.Password#">
            SELECT m.price
            FROM productvendorsmap m
            WHERE m.productid = <cfqueryparam cfsqltype="cf_sql_integer" value="#ProductId#">
            AND m.vendorid = <cfqueryparam cfsqltype="cf_sql_integer" value="#VendorIdd#">
        </CFQUERY>
        <cfcatch type="Database">
            **<cfinclude template="dbc.cfm">**
        </cfcatch>
    </cftry>

Upvotes: 1

Miguel-F
Miguel-F

Reputation: 13548

As per Travis' comments below - while the use of the cferror tag is still supported it is recommended to convert from Application.cfm to Application.cfc (if not already) and use the onError method instead. All of the restrictions that I have listed below do not apply when using the onError method and all CFML functionality is available. Here is the ColdFusion 9 documentation for the onError method.

There is an entire section of the ColdFusion documentation regarding error handling in detail. But for your specific question regarding the cferror tag you need to realize that there are limitations to what ColdFusion can do when an error occurs and even that depends on the type of error. Here is an excerpt taken from this page:

The following table lists the rules and considerations that apply to error application pages:

Validation

  • Cannot use CFML tags
  • Can use HTML tags
  • Can use the Error.InvalidFields, Error.validationHeader, and Error.validationFooter variables by enclosing them with number signs (#)
  • Cannot use any other CFML variables

Request

  • Cannot use CFML tags
  • Can use HTML tags
  • Can use nine CFML error variables, such as Error.Diagnostics, by enclosing them with number signs
  • Cannot use other CFML variables

Exception

  • Can use full CFML syntax, including tags, functions, and variables
  • Can use nine standard CFML Error variables and cfcatch variables. Use either Error or cferror as the prefix for both types of variables
  • Can use other application-defined CFML variables
  • To display any CFML variable, use the cfoutput tag


Excerpt taken from this page regarding the available error variables for each exception type:

Validation only

    error.validationHeader  Validation message header text.
    error.invalidFields     Unordered list of validation errors.
    error.validationFooter  Validation message footer text.

Request and Exception

    error.diagnostics       Detailed error diagnostics from ColdFusion.
    error.mailTo            E-mail address (same as value in cferror.MailTo).
    error.dateTime          Date and time when error occurred.
    error.browser           Browser that was running when error occurred.
    error.remoteAddress     IP address of remote client.
    error.HTTPReferer       Page from which client accessed link to page where error occurred.
    error.template          Page executing when error occurred.
    error.generatedContent  The content generated by the page up to the point where the error occurred.
    error.queryString       URL query string of client's request.

Exception only

    error.message           Error message associated with the exception.
    error.rootCause         The root cause of the exception. This structure contains the information that is returned by a cfcatch tag.
                            For example, for a database exception, the SQL statement that caused the error is in the error.RootCause.Sql variable.
                            For Java exceptions, this variable contains the Java servlet exception reported by the JVM as the cause of the "root cause" of the exception.
    error.tagContext        Array of structures containing information for each tag in the tag stack. The tag stack consists of each tag that is currently open.
    error.type              Exception type.

Note: If type = "exception", you can substitute the prefix cferror for Error; for example, cferror.diagnostics, cferror.mailTo, or cferror.dateTime.

Upvotes: 6

Adrian J. Moreno
Adrian J. Moreno

Reputation: 14859

Does your error file have a .cfm extension?

Also, if you send an e-mail with every 404, you could open yourself up to a mail bomb attack. You would probably be better adding something like Google Analytics to check for 404s. The 500 error could mean a number of things have gone wrong, so CF probably wouldn't sent an e-mail anyhow.

Upvotes: 1

Related Questions