This topic contains 0 replies, has 1 voice, and was last updated by  Casey Coleman 6 years, 6 months ago.

Possible problem with using xpcall

  • I’m trying to use calls to xpcall in order to include stack traces in my logging and I’m running into a problem.
    If the function passed to xpcall raises an error, the error is suppressed as expected but I lose subsequent annotations. I’ve included an example main function which shows this.
    Am I missing something?

    Thanks for any insight…

    function main()
       
       iguana.version();   --tested in v6.0.5
       trace(_G._VERSION);   --tested in Lua 5.1
    
       local testinputBad = 'This is not good JSON';
       local testinputGood = 
       [[{
          "Attribute1" : 123456, 
          "Attribute2" : 'A Cortado Breve', 
          "Attribute3" : 
          {
             "FirstName" : 'Ralph',
             "LastName" : 'Cramden', 
             "Wife" : 'Alice Cramden'
          }
       }]];
    
       --NOTE: both successful and unsuccessful calls to pcall act as expected.
       local Success1, result1 = pcall(json.parse, {data=testinputBad});
       local Success2, result2 = pcall(json.parse, {data=testinputGood});   
    
       trace(Success1, result1);
       trace(Success2, result2);
       
       --NOTE: when the unsuccessful call to xpcall executes ("local Success3, result3 = ...")
       --subsequent annotation stops (except for the next call to xpcall).
       --If you comment line 30 (the bad call) the subsequent annotations are restored.
    
       local Success3, result3 = xpcall(function() return json.parse{data=testinputBad}; end, ErrHandler);
       local Success4, result4 = xpcall(function() return json.parse{data=testinputGood}; end, ErrHandler);
       
       trace(Success3, result3);
       trace(Success4, result4);
    end
    
    function ErrHandler(errmsg)
       trace(errmsg);   
       return errmsg .. '\n' .. debug.traceback();   
    end
    
Tagged: 

You must be logged in to reply to this topic.