data-bindingcoldfusioncfcserver-errorcfgrid

Coldfusion: Need to disable or block "Error invoking CFC - server error" error pop-up message


I have a .cfm and .cfc that I am using to edit data in a cfgrid on the .cfm, and it works, however 10% of the time I will get the following error message:

"Error invoking CFC /test/editCFgrid.cfc: Internal Server Error"

I tried using the debugging advice, however no luck.

Here is the CFM code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<cfform name="artistform">
            <cfgrid format="html" name="artistgrid" pagesize=11
            striperows="yes" 
            bind="cfc:editCFgrid.getArtists({cfgridpage},{cfgridpagesize},{cfgridsortcolumn},{cfgridsortdirection})"
            delete="yes" insert="yes" selectmode="edit"
            onchange="cfc:editCFgrid.saveArtist({cfgridaction},{cfgridrow},{cfgridchanged})">
                <cfgridcolumn name="firstname" header="First Name" />
                <cfgridcolumn name="lastname" header="Last Name" />
                <cfgridcolumn name="address" header="Address" />
                <cfgridcolumn name="city" header="City" />
                <cfgridcolumn name="state" header="State" />
                <cfgridcolumn name="postalcode" header="Postal Code" />
                <cfgridcolumn name="email" header="Email" />
                <cfgridcolumn name="phone" header="Phone" />
                <cfgridcolumn name="fax" header="Fax" />
                <cfgridcolumn name="thepassword" header="Password" />
</cfgrid>
</cfform>
</body>
</html>

Here is the CFC code:

<cfcomponent output="FALSE">
    <cffunction name="getArtists" hint="I extract artists from the database" access="remote" output="FALSE" returntype="struct">
        <cfargument name="page" required="TRUE" hint="the page the grid is on" />
        <cfargument name="pagesize" required="TRUE" hint="records displayed per page" />
        <cfargument name="gridsortcolumn" required="TRUE" hint="selected column to sort" />
        <cfargument name="gridsortdirection" required="TRUE" hint="the sort direction" />
        <cfset var qArtists = "" />

        <cfif arguments.gridsortcolumn eq "">
            <cfset arguments.gridsortcolumn = "lastname" />
            <cfset arguments.gridsortdirection = "asc" />
        </cfif>

            <cfquery name="qArtists" datasource="test_database">
                SELECT *
                FROM Artists
                ORDER BY #arguments.gridsortcolumn# #arguments.gridsortdirection#
            </cfquery>

        <cfreturn QueryConvertForGrid( qArtists, arguments.page, arguments.pagesize ) />
    </cffunction>

    <cffunction name="saveArtist" type="any" hint="I insert, update or delete an artist" access="remote" output="FALSE" returntype="void">
        <cfargument name="gridaction" type="any" required="TRUE" hint="I for insert, U for update and D for delete" />
        <cfargument name="gridrow" type="any" required="TRUE" hint="the rows being inserted or updated" />
        <cfargument name="gridchanged" type="any" hint="the changes" />
        <cfset var qInsertArtist = "" />
        <cfset var qUpdateArtist = "" />
        <cfset var qDeleteArtist = "" />

        <cfif IsStruct( arguments.gridrow ) and IsStruct( arguments.gridchanged )>
            <cfif arguments.gridaction eq "I">
                <cfquery name="qInsertArtist" datasource="test_database">
                    INSERT INTO Artists
                        (firstname, lastname, address, city, state, postalcode, email, phone, fax, thepassword)
                    VALUES
                        (<cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.gridrow.firstname#" />, 
                        <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.gridrow.lastname#" />, 
                        <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.gridrow.address#" />, 
                        <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.gridrow.city#" />, 
                        <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.gridrow.state#" />, 
                        <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.gridrow.postalcode#" />, 
                        <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.gridrow.email#" />, 
                        <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.gridrow.phone#" />, 
                        <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.gridrow.fax#" />, 
                        <cfqueryparam cfsqltype="cf_sql_varchar" value="#arguments.gridrow.thepassword#" />)
                </cfquery>

            <cfelseif arguments.gridaction eq "U">
                <cfset var colname = StructKeyList( arguments.gridchanged ) />
                <cfset var value = StructFind( arguments.gridchanged, colname ) />
                    <cfquery name="qUpdateArtist" datasource="test_database">
                        UPDATE Artists
                        SET #colname# = <cfqueryparam value="#value#" />
                        WHERE artistid = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.gridrow.artistid#" />
                    </cfquery>

            <cfelseif arguments.gridaction eq "D">
                <cfquery name="qDeleteArtist" datasource="test_database">
                DELETE FROM Artists 
                WHERE artistid = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.gridrow.artistid#" />
                </cfquery>
            </cfif>
        </cfif>
    </cffunction>
</cfcomponent>

Each function (edit, insert, delete, etc) works, but it randomly will generate the error. Ultimately, I am seeking a code that will ignore/block/"OK" the error message so the user won't see it.

Any help will be greatly appreciated! I have spent the entire day (9 hours) googling for an answer, but I haven't found any. I do not have access to the CF Admin Log, I'm just a regular developer. Thanks!


Solution

  • There is a way to ignore/block/ok the "error" message with the following script:

    <script>ColdFusion.setGlobalErrorHandler(function (error) 
    {mygrid = ColdFusion.Grid.refresh ('artistgrid', false);
    }
    );
    </script>
    

    Insert this script in the CFM files, and you're good to go! Being that the error that I was encountering was not impacting the function (editing the cells) of the cfgrid (the pop-up message was being more of a nuisance then anything else). Hopefully this solution will help others!

    You do not need to use the 'Grid.refresh' command in the script, you can use anything command you want.

    Thanks everyone who attempted to answer my issue!