This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
anonimo182's profile picture

[Lua] Catching an error

Started by anonimo182, 19 December 2012 - 02:45 PM
anonimo182 #1
Posted 19 December 2012 - 03:45 PM
There is a way to catch the error a program gets? Like : bios [string "Test"] "=2 expected
I already tried pcall, but when a program fails, it only returns false and true, which is not what I expect

Thanks
Lyqyd #2
Posted 19 December 2012 - 03:59 PM
pcall should be returning a second argument in those cases, which would contain the error message. Perhaps you need assert instead, I can't really recall. Either way, look at the second return value.
anonimo182 #3
Posted 19 December 2012 - 04:02 PM
pcall should be returning a second argument in those cases, which would contain the error message. Perhaps you need assert instead, I can't really recall. Either way, look at the second return value.
that value always give me true, no matter what
Lyqyd #4
Posted 19 December 2012 - 04:05 PM
Clearly, it doesn't:


loadfile = function( _sFile )
    local file = fs.open( _sFile, "r" )
    if file then
        local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
        file.close()
        return func, err
    end
    return nil, "File not found"
end

dofile = function( _sFile )
    local fnFile, e = loadfile( _sFile )
    if fnFile then
        setfenv( fnFile, getfenv(2) )
        return fnFile()
    else
        error( e, 2 )
    end
end

-- Install the rest of the OS api
function os.run( _tEnv, _sPath, ... )
    local tArgs = { ... }
    local fnFile, err = loadfile( _sPath )
    if fnFile then
	    local tEnv = _tEnv
	    --setmetatable( tEnv, { __index = function(t,k) return _G[k] end } )
        setmetatable( tEnv, { __index = _G } )
	    setfenv( fnFile, tEnv )
	    local ok, err = pcall( function()
	        fnFile( unpack( tArgs ) )
	    end )
	    if not ok then
	        if err and err ~= "" then
   	         printError( err )
   		 end
	        return false
	    end
	    return true
    end
    if err and err ~= "" then
        printError( err )
    end
    return false
end

That's from bios.lua, from ComputerCraft.
ChunLing #5
Posted 19 December 2012 - 04:09 PM
You need to use multiple assignment to catch the second return. Like:
rone,rtwo = pcall(func)

That corresponds to the
local ok, err = pcall(function() fnFile(unpack(tArgs)) end ) 
part of the bios, but is a little less complicated and probably better suited to your needs.
Edited on 19 December 2012 - 03:12 PM
anonimo182 #6
Posted 19 December 2012 - 04:15 PM
You need to use multiple assignment to catch the second return. Like:
rone,rtwo = pcall(func)

That corresponds to the
local ok, err = pcall(function() fnFile(unpack(tArgs)) end ) 
part of the bios, but is a little less complicated and probably better suited to your needs.
I am using that, like this:

ok, err = pcall(os.ru({}, program)
but the second variable always return true

Or I need to use only os.run?
ChunLing #7
Posted 19 December 2012 - 04:34 PM
…you need to not use os.run(), that prevents error propagation but it doesn't return an error message. Just use "ok,err = pcall(program)"
anonimo182 #8
Posted 19 December 2012 - 04:37 PM
…you need to not use os.run(), that prevents error propagation but it doesn't return an error message. Just use "ok,err = pcall(program)"
Like:

ok, err = pcall(myProgram, my argument, soOn)
If yes, thank you! If not, please explain me how
Lyqyd #9
Posted 19 December 2012 - 06:05 PM
Yes, with the caveat that myProgram will need to be whatever loadfile (or loadstring on the contents of the file) returned.
ChunLing #10
Posted 19 December 2012 - 08:56 PM
Hmmm…how to pass the command line parameters, though? Concatenate them onto the front of the string to load?
Lyqyd #11
Posted 20 December 2012 - 03:40 AM
No, you pass arguments just like he has them on this latest example. Well, without spaces in variable names, but you know what I mean.
ChunLing #12
Posted 20 December 2012 - 05:38 PM
Oh, right, cause you pass them to pcall and not to loadstring. I derped out there.
anonimo182 #13
Posted 21 December 2012 - 08:36 AM
Well after trying, it always give me the same error:

atempt to call string
even that I am using

pcall("pahtoprogram/program", args)

Edit: Fixed it