Forum Discussion

socc3rpr0's avatar
socc3rpr0
Occasional Contributor
11 years ago

Event Handler(s)

Hello, 



How would I go about creating an event handler that will cover all my bases such as window not recognize, engine failure, posting an error to the event log, etc...?



The problem I have is that is that I need to open a TCP connection with a device under test and need to run through all my scripts, but when they do fail I want to be able to close the TCP connection before the error is posted on the event log. This would allow me to establish the connection again when I restart my scripts. I have tried using a try, catch, finally, but this does not cover errors posted to the event log, and I want to be able to just handle anything that went wrong with one event handler..is it possible? If I don't close the connection after a test(s) fail or pass then next time around I want to rerun something the connection is still open therefore I can't establish connection with the device and have to remove the ethernet cable and reinsert it after each run.



this is what I have which assumes it will pass all critira on test, but we all know that won't happen all the time.....



function main()

{



       EstablishConnection();

       Test1();

       Test2();

  

       ......

       CloseConnection();







5 Replies

  • Why not simply use



    function main()

    {

         try{

            EstablishConnection();

            Test1();

            Test2();

         }

         catch(ex){

            Log.Message("ERROR: " + ex.message);

         }

         finally{

            CloseConnection();

         }

    }





    You could also create an object to track your connection, and test against that object whenever you needed to verify if the thing even exists.  Doing so would give you more connection based information, and allow you to add your tests as methods to the connection object.



  • NisHera's avatar
    NisHera
    Valued Contributor
    Hi ,

    You may use On log error event which will fire just before error message writtent in to log.

    For example I 'm using follwing function to handle 'object does not exist' error.

    written in jScript






    function GeneralEvents_OnLogError(Sender, LogParams){


    LogParams.Locked = true; //   not to post any error message....


      if (LogParams.MessageText.indexOf('object does not exist')>-1) {


         //    doing what i need....


      }


    }



    for more information please reffer this

  • Ryan_Moran's avatar
    Ryan_Moran
    Valued Contributor


    For runtime error handling you'll need the standard try...catch... statement.







    function catchitall() {

    try {

     //this sucks to debug but doesn't stop your test

     main();

     }

    catch(e){

     Log.Error(e.message);

     }

    }





    function main()

    {



           EstablishConnection();

           Test1();

           Test2();

      

           ......

           CloseConnection();






  • Ryan_Moran's avatar
    Ryan_Moran
    Valued Contributor
    Technically neither of those examples would do what you asked in regards to reconnecting for each test. I only meant to provide an example of error handling.





    function main()

    {

         try{

            EstablishConnection();

            Test1();

               //though the connection is properly closed if Test1() fails the entire script ends Test2() is never called

            Test2();

         }

         catch(ex){

            Log.Message("ERROR: " + ex.message);

         }

         finally{

            CloseConnection();

         }

    }







    function main()

    {

         try{

            EstablishConnection();

            Test1();

         }

         catch(e){

            Log.Message("ERROR: " + e.message);

         }

         finally{

            CloseConnection();

         }

    /*

    there are, of course, numerous ways to handle this, but essentially you want to

    reconnect before calling each Test() function

    i leave it to you to determine the most elegant method with your current code structure

    */


         try{

            EstablishConnection();

            Test2();

         }

         catch(e){

            Log.Message("ERROR: " + e.message);

         }

         finally{

            CloseConnection();

         }

    }

  • socc3rpr0's avatar
    socc3rpr0
    Occasional Contributor
    Thanks for the post guys. Looks like from what I read that I will need to try a combination of the try.. Catch.. Finally... inside my main if run error occurs & use the event handler to close connection before I post to the error. I will try this tomorrow and see if it works.