Jump to content
ethanwdp

Detecting and catching an error from a function

Recommended Posts

I was trying to make my lua interpreter more convenient; instead of it printing the error to the console and the user having to alt tab over to it, it would just print it to the chat log.
Problem: xpcall is not a thing. Neither is load(). Pcall does not return an error, it only returns the status of it.
How do I catch the error in this case? I tried storing pcall as a function, example local status, luaError = pcall(thefunctionImusing, and then printing luaError. Problem is that it only prints the first line of the error, which is just kahlua defining the Java path of where it is (which, for lua debugging, is kinda useless).
So I tried local status, luaError, luaError2 = pcall(thefunctionImusing, and then printing both luaError and luaError2. And still, no luck.
 
Example of code I'm using to execute:

            elseif string.match(LMSText, "/lua ") then                local LMSTextToLua = string.gsub(LMSText, "/lua ", "")                local loadLua = loadstring(LMSTextToLua)                if pcall(loadLua) then -- loads it as a protected call, to check for errors.                    -- since there was no errors, everything is fine and this function is over                else -- otherwise, ends the function                    -- good place to have error-printing thingies here.                end            end

(It's a snippet from a huge function, which is why there's an elseif instead of an if and LMSText isn't defined. LMSText was defined much earlier in the function)
 
When I'm just using normal pcall, everything works fine, except I can't check for errors (which is the entire reason why I'm using pcall)
 
Since kahlua seems to be missing xpcall, is there an alternative I can use? Or am I using xpcall wrong?

 

Edit: Example of code I was using that should work, but with the apparent absence of xpcall isn't working:

 

 

                local LMSTextToLua = string.gsub(LMSText, "/lua ", "")
                local loadLua = loadstring(LMSTextToLua)
                function errorHandler(luaError)
                    print( "ERROR:"..luaError)
                end

                xpcall(loadLua, errorHandler)

Share this post


Link to post
Share on other sites

Have you checked if kahlua implements lua's debugger library?

Not quite sure how.

I'm having a poke around the Java as I type. I don't know what I can and can't access, I think I can only access functions in the LuaManager (correct me if I'm wrong)

Share this post


Link to post
Share on other sites

"Kahlua2 does not include any io or debug library. Some parts of the debug library may be added in the future, but the io library is better left added as a custom extension if necessary. The same thing goes for the os library, which is only partially implemented."

Looks like you are out of luck here.

Share this post


Link to post
Share on other sites

 

xpcall

Kahlua2 doesn't implement xpcall yet, but it might come in the future.

 

"Kahlua2 does not include any io or debug library. Some parts of the debug library may be added in the future, but the io library is better left added as a custom extension if necessary. The same thing goes for the os library, which is only partially implemented."

Looks like you are out of luck here.

That's troubling...

I guess I'll have to stick with having to check the console for errors.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...