Search code examples
coldfusioncoldfusion-9coldfusion-11application.cfcapplication.cfm

Coldfusion how to get function name and line number when application crashed or error


i'm working on my legacy system old code of coldfusion, is there a way i can define cfcatch in application.cfc and catch all errors of my application with

  • Function name

  • Query name

  • Line Number of code

  • Template Name

    To debug fast rather then writing everywhere in code.

application developer did not catch any error anywhere in code.i did insert cfcatch in code some of the places but still lot more to do, and because of production i don't want to modify so much of code.

im inserting cfcatch in databse and sending email to development team. because system is in production.


Solution

  • You can use the cferror tag, or onError to direct all errors to a given page/function.

    If you use cferror, the exception will be passed in the error variable. If you use OnError, it's a parameter.

    To help you along, my own error emails include the following. You will notice that we have special handling to help point out places where a blank may have been passed into a sql integer field, which happens more often than I'd like to admit.

    An error occurred: http://#cgi.server_name##cgi.script_name#?#cgi.query_string#<br />
    Time: #dateFormat(now(), "short")# #timeFormat(now(), "short")#<br />
    
    <!--- Smarter error catching for form fields --->   
    <cfif (error.message contains "Invalid data '' for CFSQLTYPE CF_SQL_INTEGER") and isdefined("form")>
        <!--- This stores a list of the Id fields --->
        <cfloop collection="#form#" item="thisField">
            <!--- Get the last two characters of the field name --->
            <cfset lastTwoChars = right(thisField, 2)>
            <!--- Get the value of the field --->
            <cfset thisFieldValue = evaluate('form.#thisField#')>
            <!--- Check to see if this is an Id field and if it's value is blank. --->
            <cfif lastTwoChars eq 'Id' and thisFieldValue eq ''>
                <h3 style="font-weight: bold; color: red">#thisField# is blank and it's possibly an integer field.</h3>
            </cfif>
        </cfloop>
    </cfif>
    
    <cfdump var="#error#" label="Error">
    
    
    <br/>
    <cfdump var="#form#" label="Form">
    <br/>
    <cfdump var="#url#" label="URL">
    <br/>
    <cfdump var="#session#" label="session">