Question : Hacking the Error with Custom error message

I am trying to insert the names in database and has created a unique on the name field, which will automatically stop the enterance of duplication key in the database.

so i get an error like:

Duplicate entry 'All Kinds' for key 2 Error Executing Database Query.

I would like that i should replace the whole sentence with the extraction of first two words!

like it should pick the first two words like DUPLICATE ENTRY and i should replca the full text with my Custom error

like this:

Duplicate entry 'All Kinds' for key 2 Error Executing Database Query.

with changes

Error! Trying to Insert same Record Twice, not allowed!

Answer : Hacking the Error with Custom error message

you can define and throw your own custom errors using <cferror>/onError() and <cfthrow>.

enclose your insert query in cftry/cfcatch block, and inside the cfcatch block throw your own error if cfcatch.type eq "Database" and cfcatch.queryError exists and starts with "Duplicate entry" words.

<cftry>
  <cfquery ...>
   your insert query here
  </cfquery>
  <cfcatch type="Database">
    <cfif cfcatch.type eq "Database" AND structkeyexists(cfcatch, 'queryError') AND refindnocase('^Duplicate entry', cfcatch.queryError)>
      <cfthrow type="duplicateEntry" message="Error! Trying to Insert same Record Twice, not allowed!" detail="some detailed info here">
    </cfif>
  </cfcatch>
</cftry>

now using <cferror> tag in your Application.cfm or onError() method in your Application.cfc, you can catch your custom duplicateEntry error type and do whatever you want with it...

Azadi
Random Solutions  
 
programming4us programming4us