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

Grabbing errors

Started by KaBooMa, 02 January 2013 - 12:36 PM
KaBooMa #1
Posted 02 January 2013 - 01:36 PM
I was wondering how would I go about grabbing a error from shell.run("randomNonsense")? I would like it do something like so…

local ran, err = shell.run("randomNonsense")
– then err would be something like…….missing symbol

Sorta like the error message you get if a program fails, but I want to run a program from a program, and get that error to report back basically. I can't really explain it I hope you understand :/ Basically I just want to grab the same error message as the default message would print, but from a program I ran from another program.
Orwell #2
Posted 02 January 2013 - 01:45 PM
You would use 'pcall' for that. It's syntax is like this:

local ok, err = pcall(function, arg1, arg2, ...)
If an error occured, the 'ok' variable will be false and the 'err' variable will contain the error message. Otherwise, the 'err' variable will contain an eventual return value (not possible from shell.run).

A simple example:

local ok, err = pcall(shell.run, "randomNonsense")

if not ok then
  print("The following error has occured:")
  printError(err)
end
Grim Reaper #3
Posted 02 January 2013 - 01:45 PM
Use the 'pcall' function to grab errors.

The first parameter returned by pcall is a boolean as to whether or not the function completed without an exception/error.
The second and all other parameters are the messages returned by the Lua interpreter or by any user defined handling of aforementioned exception/error.

Here is an example:

local isError, errorMessage = pcall(shell.run("randomNonsense"))
print(errorMessage)

In the above case, isError would be 'false' if the function DID NOT execute properly.
KaBooMa #4
Posted 02 January 2013 - 01:51 PM
I have been messing around with pcall actually…Been googling for a hour or more…tried a lot of stuff. Pcall is actually what I am using and it seems to return err. After you just said it returns multiple parameters tho, I will see what I can do there. Basically I am running a file that is coded wrongly and I am wanting to get a message about what error it has. Going to be using it in a nice little blue screen of death for something I am making ^.^

What I got rolling atm is
local ok, err = pcall(shell.run, "/KOS/test")
OS.errorCheck(ok,err)

errorcheck function just prints out a blue screen with a the err listed. it is returning as false :/ do I need to grab more args from it? like, local ok, err1, err2? or what exactly would I do?

EDIT: Because from what I can see, false isn't the problem in the code :P/> It is missing a )

I need it to print out that a ) is missing at line blah or whatever the default error reports when something like that happens

EDIT2: So I guess now it has decided to just throw me out with a error message, and not run my script -.- At random the code has decided to just throw the error and boot the program back to craftOS.
Lyqyd #5
Posted 02 January 2013 - 02:09 PM
os.run and shell.run (which itself calls os.run) both only return a single parameter. You'd be better off writing your own os.run function that does return the error message rather than printing it.
KaBooMa #6
Posted 02 January 2013 - 02:12 PM
What I figured I was going to be doing. Gonna drop the error handler in a basket for a moment and work on some other, more important aspects :)/> Thanks for the help :)/>