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

pcall(shell.run,arg) not working as expected

Started by MarioBG, 18 January 2015 - 02:20 PM
MarioBG #1
Posted 18 January 2015 - 03:20 PM
Hello!

I've just run into a critical error while finishing up the next release of my OS, and that occurs when opening files. You see, I had been opening them with pcall(shell.run,path), which allowed me to specify arguments and use shell.run from within the invoked program. However, when it comes to returning the error to the user via

local ok,contentError=pcall(shell.run,parag)
if not ok then
	olive.throwError(contentError)    --This being a function which returns the error message in a window
end
contentError is empty even if an error has occurred. Or rather, the error is not caught in the pcall statement.

What I'm asking for is a way to call a program from within another, catch any error it may output and still be able to call shell.run from within the new program. Thanks in advance!
InDieTasten #2
Posted 18 January 2015 - 04:19 PM
I'm not sure, but I think shell.run already catches any errors, so the error won't get through the shell.run function to your pcall of the shell.run function.
Meaning shell.run has the functionality of displaying the errors built in already.

And as far as I know you are able to mess with the printError function globally defined and overwrite with your olive.throwError.

Although I haven't tested this.
MarioBG #3
Posted 18 January 2015 - 04:41 PM
I'm not sure, but I think shell.run already catches any errors, so the error won't get through the shell.run function to your pcall of the shell.run function.
Meaning shell.run has the functionality of displaying the errors built in already.

And as far as I know you are able to mess with the printError function globally defined and overwrite with your olive.throwError.

Although I haven't tested this.
How would you do this? I've tried with printError=olive.throwError and it hasn't worked. Any ideas?

EDIT: Apparently, calling shell.run redeclares printError and returns its original content to it. Argh.
Edited on 18 January 2015 - 03:56 PM
InDieTasten #4
Posted 18 January 2015 - 04:51 PM
well, you could override the error function and put your own completely self written error handling in this, but it won't help your problem. I am correct with the following callstack, right?

"native"shell => yourOS => shell.run => applicationThatErrors

you would need to get rid of the shell.run so the errors wouldn't be caught until your OS.
writing your own shell.run function in your OS would be the way to implement this.

hope you are familiar with loading and doing in lua ;)/> If not let us know, I would then create some example code
MarioBG #5
Posted 18 January 2015 - 04:58 PM
well, you could override the error function and put your own completely self written error handling in this, but it won't help your problem. I am correct with the following callstack, right?

"native"shell => yourOS => shell.run => applicationThatErrors

you would need to get rid of the shell.run so the errors wouldn't be caught until your OS.
writing your own shell.run function in your OS would be the way to implement this.

hope you are familiar with loading and doing in lua ;)/> If not let us know, I would then create some example code
Please do, because I'm a self-taught coder, and as such, my learning has some huge gaps in it, particularly in the most technical stuff.
InDieTasten #6
Posted 18 January 2015 - 05:16 PM

-- untested code
function customRun(path,...)
		local l, func = loadfile(path) -- it will load the file(+ throw syntax errors)
		if(not l) then
				olive.throwError(tostring(func))
		else
				local e, m = pcall(func, ...) -- executing the code in the file with arguments
				if(not e) then
						olive.throwError(tostring(m))
				end
		end
end
Edited on 18 January 2015 - 04:21 PM
MarioBG #7
Posted 18 January 2015 - 05:57 PM
And this becomes the output to everything ( line 476 is local e, m = pcall(func, …) ). Tried declaring the function in the file that handles the desktop and the same error happens.
InDieTasten #8
Posted 18 January 2015 - 06:09 PM
Well, I guess the problem isn't necessarily with customRun function, but in the previously executed code, you seem to have overriden pcall with nil, which is a bad idea ;)/>
MarioBG #9
Posted 18 January 2015 - 06:13 PM
Well, I guess the problem isn't necessarily with customRun function, but in the previously executed code, you seem to have overriden pcall with nil, which is a bad idea ;)/>
Except I haven't, since everything else that is called using pcall works flawlessly :mellow:/>

EDIT 1: Also, when I run the file browser and then return to the desktop, it somehow works as it should. What the blazes.
EDIT 2: Tried it in a separate program on a separate computer, making the error throwing function print "Ola k ase " (Spanish meme) followed by the error message. Line 9 is still the pcall line thing.
Edited on 18 January 2015 - 05:36 PM
InDieTasten #10
Posted 18 January 2015 - 07:33 PM
sorry, messed up the return values of loadfile:

-- untested code
function customRun(path,...)
local func, e = loadfile(path) -- it will load the file(+ throw syntax errors)
if(not func) then
  olive.throwError(tostring(e))
else
  local e, m = pcall(func, ...) -- executing the code in the file with arguments
  if(not e) then
   olive.throwError(tostring(m))
  end
end
end
was kind of thinking it would behave like pcall, so with this it should work.

For more information on load and loadfile, see the reference
Edited on 18 January 2015 - 06:33 PM