Forum Discussion

marin's avatar
marin
Frequent Contributor
9 years ago

How to log error at the beginning of the OnLogError handler

Hello all,

 

I wonder if it is possible to somehow determine when the error message will be logged within the OnLogError handler?

We have following situation:

 

function foo()
{
  //error occured here;
  Log.Error("Error occured in foo");
}

function GeneralEvents_OnLogError(Sender, LogParams)
{
  Log.Message("handling error");
handlingError();
Log.Message("further handling error"); futherHandlingError(); Log.Message("initializing stuff");
initializingStuff();
//...and stop current test
Runner.Stop(true); }

 

Now in the final cumulative test log, the message from the function foo() will be placed at the very end of the log thread, sth. like this:

handling error

further handling error

initializing stuff

Error occured in foo

 

Is there a way to log the real error message at the beginning of the log thread to prevent confusion, like this:

 

Error occured in foo
handling error

further handling error

initializing stuff

 

 

Many thanks for any hints.

 

 

  • Well, the definition of the OnLogError Event http://support.smartbear.com/viewarticle/68044/ says

    "The event occurs before an error message is posted to the test log."

     

    so I don't think you can redefine that.

     

    If you just want to make things easier to read in the log, try putting Append Log Folder at the top of the error handler function and Pop Log Folder at the bottom.  This will indent the error handling and organize the log a little more.  

     

    See attached screenshots.

     

     

     

     

     

  • Hi Marin,

     

    The possible (though not verified) implementation might be like this:

    -- create a public flag that indicates if the code is in the error handler or not;

    -- post an error from the eror handler;

    -- continue wih the other stuff in the error handler.

     

    I.e. something like this:

     

    var boolIsInErrorhandler = false;

    function GeneralEvents_OnLogError(Sender, LogParams) {
    if (boolIsInErrorHandler) then
    return;

    boolIsInErrorHandler = true;
    Log.Error(<initial error from LogParams>)
    boolIsInErrorHandler = false;
    Log.Message("handling error");
    handlingError();
    Log.Message("further handling error"); futherHandlingError(); Log.Message("initializing stuff");
    initializingStuff();
    //...and stop current test
    Runner.Stop(true); }
  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi Marin,

     

    The possible (though not verified) implementation might be like this:

    -- create a public flag that indicates if the code is in the error handler or not;

    -- post an error from the eror handler;

    -- continue wih the other stuff in the error handler.

     

    I.e. something like this:

     

    var boolIsInErrorhandler = false;

    function GeneralEvents_OnLogError(Sender, LogParams) {
    if (boolIsInErrorHandler) then
    return;

    boolIsInErrorHandler = true;
    Log.Error(<initial error from LogParams>)
    boolIsInErrorHandler = false;
    Log.Message("handling error");
    handlingError();
    Log.Message("further handling error"); futherHandlingError(); Log.Message("initializing stuff");
    initializingStuff();
    //...and stop current test
    Runner.Stop(true); }
    • marin's avatar
      marin
      Frequent Contributor

      Hello Alex,

       

      many thanks for the excellent suggestion, this is indeed exactly what I was looking for.

      Eventually I ended up with your and Marsha's ideas combined -> error log from the corresponding method before start of error handler and error handler's own logs in a separate sub folder.

       

      Many thanks again,

       

      Marin

  • What is it that you are considering to be the "real error message"?

    • marin's avatar
      marin
      Frequent Contributor

      Hello Marsha,

       

      maybe I did not choose the right description: what I meant was that the error message from the function foo() ("Error occurred in foo") should be logged first - before all logs from the OnLogError handler.

      Does it make sense?

       

      Thx,

       

      Marin

      • Marsha_R's avatar
        Marsha_R
        Moderator

        Well, the definition of the OnLogError Event http://support.smartbear.com/viewarticle/68044/ says

        "The event occurs before an error message is posted to the test log."

         

        so I don't think you can redefine that.

         

        If you just want to make things easier to read in the log, try putting Append Log Folder at the top of the error handler function and Pop Log Folder at the bottom.  This will indent the error handling and organize the log a little more.  

         

        See attached screenshots.