0

I seem to be having an issue with the photo uploading function on a site I'm working on.

<cftry>

                <cffile
                    action="upload"
                    destination="#physicalPath#\PHOTOS"
                    filefield="photo"
                    nameconflict="makeunique"                       
                    accept="image/jpeg, image/pjpeg" 
                    result="mypicture"
                />                                                    


     <cfset OutcomeID = "#FORM.OutcomeID#"/> 
              <!---Inserts file name and outcome id into Uploads table in the database --->
  <cfinvoke component="#classpath#.cf_classes.dao.uploads" method="insertPhoto" returnvariable="success">
    <cfinvokeargument name="fileName" value="#mypicture.SERVERFILE#"/>
    <cfinvokeargument name="OutcomeID" value="#OutcomeID#"/>
    </cfinvoke>

      <cfset photoError = "Photo uploaded successfully"/>
<cfcatch type="any">
      <cfset photoError = "There was an error uploading your photo.<br/> Please make sure it is a JPEG format."/>
   </cfcatch>
</cftry>

No matter what, I just keep getting back the error message defined in the cfCatch. What are some good ways to troubleshoot something like this? Do any problems stand out to anyone?

Thanks for the help, I'm still relatively new to CF.

aceslowman
  • 621
  • 13
  • 28
  • 2
    I suggest not using TRY/CATCH until you have debugged 99% and totally understand what's going on. Does your uploader work perfectly fine without the TRY/CATCH? – Evik James May 16 '12 at 19:35
  • Well I guess I know what to do in this situation now! That should be enough to get me on my way. Thanks. – aceslowman May 16 '12 at 19:47
  • 1
    In my opinion, TRY/CATCH should be used only when you have thoroughly debugged everything and are just trying to render a better user experience if someone happens to encounter a situation that you couldn't foresee or test for. – Evik James May 16 '12 at 19:58
  • instead of _removing_ try/catch from your code, i suggest you add cfcatch.message (and possibly cfcatch.detail as well) to the error message returned by cfcatch. for example: – azawaza May 17 '12 at 00:33

1 Answers1

1

You're masking the exception that your cfcatch is catching by just setting a string in there. If you don't want to remove your exception handler, a quick and easy way to see what the underlying error is while you're still working on the code is to add:

<cfcatch><cfdump var="#cfcatch#"></cfcatch>

You may want to add an abort="true" to your cfcatch to stop processing of further code at that point which can mask your exception dump or cause it not to appear.

Putting cfcatch.message into a string that the user sees is not good practice as this may potentially result in information disclosure which can be a security risk (eg. if the message contains a file path on your server or similar)

Long term, if you're using Coldfusion Builder then you can also use the Coldfusion Debugger that is built in to step through your code and see where it is failing at the state of your variables at that point.

Marcin
  • 802
  • 8
  • 16
  • +1 on the debugger. It's not quite as easy to set up as the java debugger, but it's not that hard and long-term its definitely the way to go. – barnyr May 17 '12 at 12:12
  • Although to be honest I actually gave up on trying to get the CF Debugger working properly and we bought [FusionDebug](http://www.fusion-debug.com/fd/) licenses which works a treat. Had issues with Adobe debugger suddenly disconnecting or not hitting breakpoints, etc. Will have to try again with CF10 though. – Marcin May 18 '12 at 04:09