Question
· Jan 3

CSP Page is catching Hyper event error when trying to call server side method

Hi Community,

I have a CSP page I am developing with the intent of pulling files from an SFTP site and placing them in a folder on the local network.

I am having an issue where the CSP page is catching a JS error. The error in the browser is - A JavaScript exception was caught during execution of HyperEvent: SyntaxError: Unexpected identifier 'downloading'. (However, the unexpected identifier changes after refreshing page, or after removing the Write statement with the work 'downloading' in it.)

I think I have narrowed it down to a piece of the Javascript code - 

#server(..DownloadFiles(document.getElementById('fileList').value))#;

It doesn't seem to like this call to a back end method (..DownloadFiles) where the argument is (fileList). Can someone advise me if there is a better way to do this that will not cause the error? 

Thanks,

Dan

Also, if required, I can upload more of the code used.

Discussion (9)2
Log in or sign up to continue

Hi Enrico,

Thanks for your reply.

So currently this is all in a single CSP page (HTML, then JS, then ObjectScript). I have a button that when clicked, calls the JS function onClick=handleFormSubmit().

Then within the JS handleFormSubmit() function, this is where  #server(..DownloadFiles(document.getElementById('fileList').value))#;                                                                        is called, which invokes the method download files, which is in <script> tags in the same CSP page. The tag looks like - 

<script language = "Cache" method = "DownloadFiles" RunAt="Server" ARGUMENTS = "fileList:%String">

Hopefully this is helpful, but if you need more info, let me know.

I do, in this case, the code is - 

            Set sc = sftp.Get(remoteFile, localFile, "0666")
                        Set sc = sftp.Get(remoteFile, localFile, "0666")
            If $$$ISERR(sc) {
                Write "Error downloading file: ", fileName, !
            else {
          Write "File Successfully Downloaded", fileName, !
            }

However, if I remove the Write commands, this error goes through every Write command and I am not sure how else to log this for debugging issues.

Remember, what you write in/from that method is javascript.

I think the simplest solution for debugging is using javascript alert(), another option can be injecting your debug info into page html/dom using javascript (this depend on page structure).
For simple alert() you can:

Set sc = sftp.Get(remoteFile, localFile, "0666")
If $$$ISERR(sc) {
    &js< alert(#(..QuoteJS("Error downloading file: "_fileName_" -> "_$system.Status.GetErrorText(sc)))#);>
} else {
   &js< alert(#(..QuoteJS("File Successfully Downloaded"_fileName))#);>
}

Thanks Enrico, apologies for the delayed response. I have implemented the changes you suggested and I am still getting the HyperEvent JS exception, but now it just says - SyntaxError: Invalid or unexpected token.

The weird part is, the objectscript code is executing and downloading the file from the SFTP as expected, just catching the HyperEvent exception as well, which seems odd to me.

Thanks for all you help so far, and hopefully you may be able to help further.

As I suspected, in the DownloadFiles method the variable "fileName" is not (yet) defined when you use it.
In addition, in your catch method you have a Write command that "Write" invalid javascript code.

I'd suggest to change the catch code with:

} Catch ex {
    Set sc = ex.AsStatus()
    &js<..QuoteJS("Exception: "_ex.DisplayString());>
}

This way if an objectscript error you get an alert with the error details.

Then, change all reference to the "fileName" variable and use "fileList" instead before the line:

For i=1:1:$LISTLENGTH(fileArray)

This prevents <UNDEFINED> errors when referencing "fileName" variable.