Search code examples
coldfusionerror-handlingcoldfusion-10remoteobject

Handle Invalid Data Type in CF Remote Function


So I have a remote ColdFusion Function like:

remote string function name (required numeric varname){

This is accessed via AJAX call. Google has taken it upon itself to pass in junk/blank values to the URL to this remote function. How can I gracefully handle those for Bots/Users to manage to get in a junk value. I've tried putting try/catch around/inside the function and doesn't work. I've also tried setting a default value but I still get an error. I'd like to be able to return an error message.

Thoughts?

Right now:

domain.com/path/to/page.cfc?method=function&varname=

Is throwing an error

domain.com/path/to/page.cfc?method=function&varname=5

Is working as expected.


Solution

  • Update:

    I am leaving this here for posterity, as it explains the cause of the error and chain of events with validation. However, Adam's response is the correct solution IMO.


    remote string function name (required numeric varname){

    I've tried putting try/catch around/inside the function and doesn't work.

    Because the argument value is validated before CF executes anything inside the function. So it never even gets to the try/catch.

    If you want to allow non-numeric values, you must set the argument type to string and perform validation inside the function. ie

          // use whatever check is appropriate here
          if ( IsNumeric(arguments.varname) ) { 
              // good value. do something
          }
          else {
              // bad value. do something else
          }
    

    I've also tried setting a default value but I still get an error

    domain.com/path/to/page.cfc?method=function&varname=

    Update The reason it does not work is because the varname parameter does exists. Its value is an empty string. As long as some value is passed (even an empty string) the default is ignored.