5 Replies Latest reply on Nov 30, 2016 8:54 AM by erolst

    Get Last Error Script

    ZoocMan85

      Easy quick question, im trying to write a script that if i get an error code 1631 to display a custom dialog,  In my script its written

       

      Set Error Capture On

      if get (LastError) ="1631"

      Show Custom Dialog.

       

      That doesnt seem to work, am i writing this wrong ?

        • 1. Re: Get Last Error Script
          coherentkris

          error codes are integers not strings

          Get(LastError)

          "1631" is not equal to 1631

          The placement of your error capture is important as well.

          do something...

          set var $error = Get (LastError)

          If ( $error = 1631; Show Custom Dialog ; do nothing)

          • 2. Re: Get Last Error Script
            ZoocMan85

            This also worked to  Get (LastError) <> 1631

            1 of 1 people found this helpful
            • 3. Re: Get Last Error Script
              mikebeargie

              You can't just arbitrarily run get(lasterror), it has to be run immediately after where you are trapping for errors.

               

              A use case for 1631 (connection failed) would be:

              Set Error Capture [ on ]

              Insert From URL [ table::field ; "notaurl" ]

              If [ Get(LastError) = 1631 ]

                 Show Custom Dialog [ "Insert Failed" ]

              End If

               

              Alternatively, you can do a generic error check as well:

              Set Error Capture [ on ]

              Insert From URL [ table::field ; "notaurl" ]

              If [ Get(LastError) <> 0 ]

                Show Custom Dialog [ "Error Occurred" ]

              End If

               

              And you can even make a collection of errors for an entire script:

              Set Error Capture [ on ]

              Insert From URL [ table::field ; "notaurl" ]

              Set Variable [ $errors ; List ( $errors ; if ( Get(LastError) <> 0 ; Get(LastError) ; "" ) ) ]

              Insert From URL [ table::field ; "also not a URL" ]

              Set Variable [ $errors ; List ( $errors ; if ( Get(LastError) <> 0 ; Get(LastError) ; "" ) ) ]

              If [ ValueCount( $errors ) > 0 ]

                 Show Custom Dialog [ "Error Occurred" ; $errors ]

              End If

               

              What you are doing is setting error capture on AFTER you have already performed an action, so it's not capturing errors at all. Error Capture [ on ] should be BEFORE any script steps you want to trap errors on. And then you should be looking for specific errors immediately after those steps using one of these methods.

              • 4. Re: Get Last Error Script
                mikebeargie

                Please read my post about proper error checking, it's an important technique to understand and use correctly.

                • 5. Re: Get Last Error Script
                  erolst

                  You should capture the error code you're interested in immediately, before doing something else.

                   

                  Also, activate Error Capture in the calling script, like:

                   

                  Set Error Capture [ on ]

                  # do the interesting stuff

                  Set Variable [ $lastError ; Get ( LastError ) ]

                  Perform Script [ ErrorDialog ; parameter: $lastError ]

                  ...

                   

                  and you need to pass that value to the subscript, like:

                   

                  Set Variable ( $lastError ; Get ( ScriptParameter ) ]

                  If [ $lastError = 1631 ]

                     Show Custom Dialog [ ]

                  End if

                   

                  And last but not least, it's an error number, not a string, so don't put it into quotes. It should still work since FM is at times quite lax with data types, but still.