Search code examples
c#error-handlingluasyntax-errorluainterface

Handle syntax errors in LuaInterface


I am trying to integrate a Lua Scripting interface into my C# project by using LuaInterface.

It works as expected if I execute syntactically correct code, but as soon as a syntax error (or any other error as it seems) is introduced to the script an SEHException gets thrown without any information regarding the error.

A simple example to trigger the behavior: new LuaInterface.Lua().DoString("die");

That of course completely nullifies Lua's error handling mechanisms and is a show-stopper for me.

Apparently this is a known bug which is open since 2011.

Are there any workarounds, a version of LuaInterface without this bug or is there an alternative lua wrapper which correctly handles errors?


Solution

  • Lua errors (syntax or runtime errors) encountered by DoString should result in a LuaException being thrown. Its message will contain the error string generated by Lua. For instance, given your example:

    try
    {
        new LuaInterface.Lua().DoString("die");
    }
    catch (LuaException ex)
    {
        Console.WriteLine(ex.Message);
    }
    

    You should get the following error:

    [string "chunk"]:1: '=' expected near '<eof>'
    

    You don't show any context for your one liner. Is it wrapped in a try-catch block?

    If so, perhaps it's a bug?