ftp package inside xltp – can’t catch errors

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf ftp package inside xltp – can’t catch errors

  • Creator
    Topic
  • #53051
    Peter Heggie
    Participant

      I need to ftp files at runtime when processing a message in a translate; I don’t know what the files are until runtime.

      If I purposely mispell the file name, it should fail – and I do get the error messages written to stdout (which is annoying and someday I’ll figure out how to intercept it) – but the [catch ..] does not work.

      Am I using [catch..] wrong?

      Code:

      if {$debug} {echo “[gts] $module FTP CD to dir $dir successful”}

         # ftp get requested file
         set result “”
      if {[catch {::ftp::Get $handle  $file -variable content} result]} {
           if {$debug} {echo “[gts] $module FTP Get result $result”}
           ::ftp::Close $handle
           if {$debug} {echo “[gts] $module failed to complete FTP Get of file $file”}
           return “-1failed to complete FTP Get of file $file”
         }
      if {$debug} {echo “[gts] $module FTP get file $file data successful”}

      I’m getting the ‘successful’ debugging statement but I’m not getting the ‘error’ debugging statement.

      Code:

      12/04/18 15:42:29.023 html_admission_notification.get_ftp_data FTP CD to dir PCPNotify successful
      error     error | E: Error retrieving file “AdmissionN_otification.html”!:
      error     invalid command name “tix”
      error         while executing
      error     “tix option get fixed_font”
      12/04/18 15:42:29.025 html_admission_notification.get_ftp_data FTP get file AdmissionN_otification.html data successful

      If I try again with the correct file name, it works (I can see the content):

      Code:

      12/04/18 15:47:09.537 html_admission_notification.get_ftp_data FTP CD to dir PCPNotify successful
      12/04/18 15:47:09.736 html_admission_notification.get_ftp_data FTP get file AdmissionNotification.html data successful

      Or can someone suggest a way for me to detect the FTP runtime error in my code?

      Peter

      Peter Heggie
      PeterHeggie@crouse.org

    Viewing 3 reply threads
    • Author
      Replies
      • #76370
        John Mercogliano
        Participant

          Peter,

           The ftp get command returns a 1 if successful or 0 if not.  An invalid file name would return a 0.  So you should just need the if statement without the catch.  

            The catch is not working because the ftp get command is not actually failing.  The error information you are seeing is the standard output from the ftp command that is being captured to the file variable.

          John Mercogliano
          Sentara Healthcare
          Hampton Roads, VA

        • #76371
          Peter Heggie
          Participant

            John,

            I just updated my code:

            Code:

               if {$debug} {echo “[gts] $module FTP CD to dir $dir successful”}

               # ftp get requested file
               set rcode [::ftp::Get $handle  $file -variable content]
               if {$debug} {echo “[gts] $module FTP Get rcode $rcode”}
               if {$rcode} {
                 if {$debug} {echo “[gts] $module FTP Get rcode $rcode”}
                 ::ftp::Close $handle
                 if {$debug} {echo “[gts] $module failed to complete FTP Get of file $file”}
                 return “-1failed to complete FTP Get of file $file”
               }

               if {$debug} {echo “[gts] $module FTP get file $file data successful”}

            This is the result:

            Code:

            12/04/19 10:37:42.055 html_admission_notification.get_ftp_data FTP CD to dir PCPNotify successful
            error     error | E: Error retrieving file “admission_notification.html”!:
            error     invalid command name “tix”
            error         while executing
            error     “tix option get fixed_font”
            12/04/19 10:37:42.057 html_admission_notification.get_ftp_data FTP Get rcode
            [xlt :xlat:ERR /0:sms_pharm_xlate:04/19/2012 10:37:42] [0.0.10970268] Xlate ’email.xlt’ failed: Tcl callout error

                           html_admission_notification
                       :

            errorCode: NONE

            errorInfo:
            expected boolean value but got “”
               while executing
            “if {$rcode} {
                 if {$debug} {echo “[gts] $module FTP Get rcode $rcode”}
                 ::ftp::Close $handle
                 if {$debug} {echo “[gts] $module failed …”
               (procedure “get_ftp_data” line 35)
               invoked from within
            “get_ftp_data $server $userid $password $directory $filename $debug $module”
               (procedure “html_admission_notification” line 33)
               invoked from within
            “html_admission_notification”

            Is my ftp namespace syntax incorrect? At the top of the proc I executed a package require ftp.

            Also, is there a way to intercept the logger output to put it into a variable I can access?

            Pete

            Peter Heggie
            PeterHeggie@crouse.org

          • #76372
            John Mercogliano
            Participant

              For logging info here is the basics when using the ftp package

              Code:


              package require ftp
              set fdLog [open w+]
              # The ftp command uses the log package to write out debug messages
              # This next command ensures that all levels are unsuppressed.
              # Had a problem that the debug info was not printing because it was being
              # suppressed somewhere in one of the packages
              ::log::lvSuppressLE emergency 0
              # This next command associates the log command to our log file so it will print there instead of
              # to stdout and stderr
              ::log::lvChannelForall $fdLog
              # set this to one if you are having problems and need more info about what ftp is doing
              set ::ftp::DEBUG 1
              # This is set to one to we can print out the actual ftp communication with the ftp server.
              set ::ftp::VERBOSE 1
              .
              .
              Rest of ftp coding

              If you look at the if statement, remember rcode will be set to 1 if it is successful so you need to check for false not true on the if .

              I have never tried using the ftp command from within a translation before but I use it successfully from cron and within tps.  Without more info on how you are attempting to us it, I don’t have any more thoughts.

              John Mercogliano
              Sentara Healthcare
              Hampton Roads, VA

            • #76373
              Peter Heggie
              Participant

                Right, but $rcode should be either 0 or 1 after the call, and my debugging output shows that it is not set.

                I have a translate that has a Copy statement with a tcl xltp; this xltp calls a separate (file) tcl proc. It is this called proc that executes the FTP.

                I followed your example for modifying the log package behavior, by setting a command for messages at ‘error’ level:

                Code:

                global ftperr
                set ftperr “0”

                package require log
                ::log::lvCmd error ftperrproc

                (other ftp code)

                # ftp get requested file
                set rcode [::ftp::Get $handle  $file -variable content]
                if {$debug} {echo “[gts] $module FTP Get ftperr $ftperr”}
                if {$ftperr ne “0”} {
                     ::ftp::Close $handle
                     if {$debug} {echo “[gts] $module $ftperr”}
                     return “-1$ftperr”
                }

                (other code)

                # end of proc
                }

                proc ftperrproc {lvl txt} {
                 global ftperr
                 if {[string first ” E: ” $txt] > -1} {
                   set ftperr [lindex [split $txt “:”] 1]
                 }
                }

                This worked. I’m not comfortable with making things global but I could not get upvar to work.

                output:

                Code:

                12/04/19 15:38:12.080 html_admission_notification.get_ftp_data FTP CD to dir PCPNotify successful
                12/04/19 15:38:12.082 html_admission_notification.get_ftp_data FTP Get ftperr  Error retrieving file “admission_notification.html”!
                12/04/19 15:38:12.083 html_admission_notification.get_ftp_data  Error retrieving file “admission_notification.html”!
                12/04/19 15:38:12.123 html_admission_notification current directory /test/cis5.8/chonltst/exec/processes/sms_pharm
                12/04/19 15:38:12.123 html_admission_notification error in FTP; returning to Xlate

                So I will have to look for the E: eyecatcher, but thats good as long as I can depend on that for errors.

                Thank you for your help. I’ll be able to use this technique for other situations with imported package runtime errors.

                Now I just have to intercept the error function raised from the xltp to the xlate, maybe I should not use the error function, but just pass a known value to the xlateOutVals and intercept that.

                Peter

                Peter Heggie
                PeterHeggie@crouse.org

            Viewing 3 reply threads
            • The forum ‘Cloverleaf’ is closed to new topics and replies.