0
votes

I have cfcatch block that should catch any exception. Once error detected I build custom function that takes NativeErrorCode as an argument. If error code is one that I'm looking for that indicates duplicate/PK violation I have custom message that will return to the user. If error code is not one that I'm looking for then global message will be returned. However, I run in the problem where ColdFusion returned error message that NativeErrorCode does not exist. I know that Native Error Code is reserved for database type. Is there a way to check the type and prevent this problem or there is better way to fix this issue? Here is my code example:

<cftry>
    // Stored procedure call
    <cfcatch type="any">
        <cfset local.fnResults = {status : "400", message : Application.functions.errorCatch(cfcatch.NativeErrorCode)}>
    </cfcatch>
</cftry>

public string function errorCatch(required string ErrorCode) {
    local.message = "";

    if(arguments.ErrorCode EQ 2627){
        local.message = "Error! Cannot insert duplicate value.";
    }else{
        local.message = "Error! Please contact your administrator.";
    }

    return message;
}

You can see above how my errorCatch function works and what code I'm checking. I still want cfcatch to grab any exception in my code not just database errors.

1
It's all there in the documentation. #cfcatch.type# helpx.adobe.com/coldfusion/cfml-reference/coldfusion-tags/… - haxtbh
I would say that a more detailed error message should be sent to the developer or administrator of this system, but the end user doesn't need to know much more than "Oops. Something didn't work.". They don't even need to know that a dupe value was submitted. 500 errors are frustrating for someone who actually needs to troubleshoot the error, but are perfect for end users. Also, this is an error that can be logged in the database from the database that threw the error. There's not much need to bring it all the way back to ColdFusion. - Shawn
@Shawn Well opinions are divided. I heard opposite arguments as well. I'm planning to catch all errors with onError function in Application.cfc then insert that in Database table. - espresso_coffee
And you also probably don't want to just rely on the end user to contact the admin. Or to send an email to the admin/dev. That's how errors go silent and take forever to get fixed. Log it in the database and then have the database send it to the appropriate people or your error management system, or have an app that can check errors. - Shawn
Opposite arguments to just logging it in the database? Or opposite arguments to how much information you share with the end user? To either one, I'd say that probably more opinions should be gathered and you'll probably find the common advice being to minimize what you send to the end user and to minimize traffic on your network and app. - Shawn

1 Answers

2
votes

Two ways come to mind for handling your branching catch logic, have 2 catch blocks, or check the catch object has the data you want.

In my first example I added a catch block exclusively for database errors. If the type of the error is database a Native Error Code will be included or be -1 if the database driver doesn't include one. For the any argument, I just added your default return string. You may want to have custom logic that would handle non-database type exceptions.

<cftry>
    // Stored procedure call
    <cfcatch type="database">
       <cfset local.fnResults = {status : "400", message : Application.functions.errorCatch(cfcatch.NativeErrorCode)}>
    </cfcatch>

    <cfcatch type="any">
        //Non database related error
        <cfset local.fnResults = "Error! Please contact your administrator.">
    </cfcatch>
</cftry>

In my second Example I just updated your errorCatch function with a check that a NativeErrorCode exist before we try to pass it.

    <cfcatch type="any">
        //Passing the default error code value, you may want custom logic here
        <cfset local.fnResults = {
            status : "400", 
            message : Application.functions.errorCatch( cfcatch.keyExists("NativeErrorCode")?cfcatch.NativeErrorCode:-1)
        }>
    </cfcatch>