Reporting error messages

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Reporting error messages

Robert Osfield
Hi,

I rather new to Lua, and have slowly progressed to the point of successfully integrating C++ and Lua via Swig, and have a 3D app running with an init and frame being all controlled from Lua scripts.  Still rather simple scripts and simple C++ classes but progress no less. 

The bit I'm currently perplexed by is how to report back errors that occur in the Lua scripts - when errors, in my C++ code I have:

   int result = luaL_dofile(L, "frame.lua");

And when the script runs fine I get a 0, and when there is an error I get 1, which is all go so far, but... where do I go next to report the details of the error, such as the line number, and the error message?  I have searched the lua websites and headers and can't find anything to point me in the right direction.

Thanks in advance for you help,
Robert.
Reply | Threaded
Open this post in threaded view
|

Re: Reporting error messages

Alex Queiroz
Hallo,

On 5/11/06, Robert Osfield <[hidden email]> wrote:
>  The bit I'm currently perplexed by is how to report back errors that occur
> in the Lua scripts - when errors, in my C++ code I have:
>
>     int result = luaL_dofile(L, "frame.lua");
>

     You should first load you script, and then run it with
lua_pcall(). This way you can catch errors and report them.

--
-alex
http://www.ventonegro.org/
Reply | Threaded
Open this post in threaded view
|

Re: Reporting error messages

Luiz Henrique de Figueiredo
> >
> >     int result = luaL_dofile(L, "frame.lua");
> >
>
>      You should first load you script, and then run it with
> lua_pcall(). This way you can catch errors and report them.

That's what luaL_dofile does. If result is  not 0, then the error message
is at the top of the stack and can b retrieved with lua_tostring(L,-1).
--lhf
Reply | Threaded
Open this post in threaded view
|

Re: Reporting error messages

Robert Osfield
On 5/11/06, Luiz Henrique de Figueiredo <[hidden email]> wrote:
> >
> >     int result = luaL_dofile(L, "frame.lua");
> >
>
>      You should first load you script, and then run it with
> lua_pcall(). This way you can catch errors and report them.

That's what luaL_dofile does. If result is  not 0, then the error message
is at the top of the stack and can b retrieved with lua_tostring(L,-1).
--lhf

Thanks for pointing me in the right direction, such a simple solution once you know how ;-)

For other newbee wondering about this, the follow code works just fine:

    if (luaL_dofile(L, "frame.lua")!=0)
    {
        printf("Error: %s\n",lua_tostring(L,-1));
    }

Reply | Threaded
Open this post in threaded view
|

Re: Reporting error messages

Fabian Peña
In reply to this post by Robert Osfield
You can register you own error report function
ej for windows

int lalert(lua_State *L)
{
        MessageBox(NULL,lua_tostring(L,-1),"Script Error",MB_OK);
        return 0;
}

and under your lua initialization insert


lua_register(L,"_ALERT",lalert);


Robert Osfield wrote:

> Hi,
>
> I rather new to Lua, and have slowly progressed to the point of
> successfully integrating C++ and Lua via Swig, and have a 3D app running
> with an init and frame being all controlled from Lua scripts.  Still
> rather simple scripts and simple C++ classes but progress no less.
>
> The bit I'm currently perplexed by is how to report back errors that
> occur in the Lua scripts - when errors, in my C++ code I have:
>
>    int result = luaL_dofile(L, "frame.lua");
>
> And when the script runs fine I get a 0, and when there is an error I
> get 1, which is all go so far, but... where do I go next to report the
> details of the error, such as the line number, and the error message?  I
> have searched the lua websites and headers and can't find anything to
> point me in the right direction.
>
> Thanks in advance for you help,
> Robert.

Reply | Threaded
Open this post in threaded view
|

Re: Reporting error messages

Luiz Henrique de Figueiredo
> lua_register(L,"_ALERT",lalert);

Not in Lua 5.1...