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

Os.run return help

Started by GamersForOne, 02 March 2015 - 03:35 PM
GamersForOne #1
Posted 02 March 2015 - 04:35 PM
Hi!
I'm currently trying to make a shop with monitors and computers, and I get errors many times. I know how to solve the errors, but I'm tired of going behind the monitor and click the computer to see the error. I'm using os.run in startup to make it work btw.
This is how it looklikes:

while true do
  local success = os.run({}, "program");
  print("Press monitor or enter to restart");
  local event,par1,par2,par3 = os.pullEvent();
  while not (event == "monitor_touch" or (event == "key" and par1 == 28)) do
	event,par1,par2,par3 = os.pullEvent();
  end
  term.setBackgroundColor(colors.black);
  term.clear();
  term.setCursorPos(1,1);
  term.setTextColor(colors.yellow);
  term.write("CraftOS 1.7");
  term.setCursorPos(1,2);
  term.setTextColor(colors.white);
  sleep(0.1);
end
But I want to add so that when os.run returns false, I can get the error and print it on the monitor. like

while true do
  -- Added errorStr
  local success,errorStr = os.run({}, "program");
  print("Press monitor or enter to restart");
  local event,par1,par2,par3 = os.pullEvent();
  while not (event == "monitor_touch" or (event == "key" and par1 == 28)) do
	event,par1,par2,par3 = os.pullEvent();
  end
  -- New Code Starts
  if not (success) then
	local mon = peripheral.wrap("right");
	mon.print(errorStr);
  end
  -- New Code Ends
  term.setBackgroundColor(colors.black);
  term.clear();
  term.setCursorPos(1,1);
  term.setTextColor(colors.yellow);
  term.write("CraftOS 1.7");
  term.setCursorPos(1,2);
  term.setTextColor(colors.white);
  sleep(0.1);
end
So I want the os.run to return (true,nil) or (false,"<error string here>").
Is there a way to get that error message and if so, how?
Lupus590 #2
Posted 02 March 2015 - 05:49 PM
you want to use

function func()
	return os.run({},"program") --why are you using os.run? most people use shell.run
end

local ok,return_message = pcall(func) --note the lack of () after func in pcall

where ok is a bool which is false if there was an error in the function
and return_message is the value that is returned by the function (assuming that it exited correctly)

I'm not 100% sure but return_message may give you the error message (a string) if error is true


Edit: I've been told that I got it wrong
Edited on 02 March 2015 - 07:15 PM
KingofGamesYami #3
Posted 02 March 2015 - 06:16 PM
Lupas590 is incorrect. You cannot catch an error using os.run. Ever. However, pcall can be used on functions, which we can turn a program into with very little effort.


local file = fs.open( "filetorun", "r" )
local data = file.readAll()
file.close()
local func, err = loadstring( data ) --#catching compile errors
if not func then
  --#err is an error message
  error( err )
else
  --#func is a function
  local ok, err = pcall( func ) --#catching runtime errors
  if not ok then
    --#err is an error
    error( err )
  end
end
GamersForOne #4
Posted 03 March 2015 - 09:21 AM
Lupas590 is incorrect. You cannot catch an error using os.run. Ever. However, pcall can be used on functions, which we can turn a program into with very little effort.


local file = fs.open( "filetorun", "r" )
local data = file.readAll()
file.close()
local func, err = loadstring( data ) --#catching compile errors
if not func then
  --#err is an error message
  error( err )
else
  --#func is a function
  local ok, err = pcall( func ) --#catching runtime errors
  if not ok then
	--#err is an error
	error( err )
  end
end

Now I've added that code, It successfully creates a function with loadstring and executes the program with that function and it works fine. But back to my question, what I wanted was a way to catch the error like in a try catch statement from Java, C# etc, and for some reason it crashes normally, It still executes the function I made, but when I do pcall(func) and it encounter an error, CraftOS itself prints the error and break the code. And that's not what I want. I want it to quit the function func and return false + the error string that it got by the error. I made the error on purpuse btw, so I know how to fix the error. I just want the try catch thingy to work propery.
Edited on 03 March 2015 - 08:22 AM
Lupus590 #5
Posted 03 March 2015 - 10:06 AM
try this: http://www.computercraft.info/forums2/index.php?/topic/9964-error-handling-in-lua/
MKlegoman357 #6
Posted 03 March 2015 - 04:17 PM
If you just copied KindofGamesYami's code without modifying then of course it will error because that's what it's meant to do, look at the 'error' functions. Just replace the calls to 'error' with your own code to print those errors.
ElvishJerricco #7
Posted 03 March 2015 - 04:22 PM
Wouldn't the easy way to do this be redirecting the terminal to the monitor with term.redirect()? Or is it important that the program output to both the terminal and the monitor separately?

Anyway the easiest way to do this besides that would be dofile("program") in a pcall.


local ok, err = pcall(dofile, "program")
if not ok then
    printToMonitor(err)
end
Dragon53535 #8
Posted 04 March 2015 - 01:06 PM
And that's not what I want. I want it to quit the function func and return false + the error string that it got by the error. I made the error on purpuse btw, so I know how to fix the error. I just want the try catch thingy to work propery.
That's what it's actually doing, as MKLegoman stated, you can replace error with your own thing. The thing is with code that actually errors, it kinda errors, and the function error() kills the current code, and presents an error message, that it's sent. WHICH is err, err is your string error message, which you can handle how you will, without actually putting it in error()