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

pcall error catching

Started by nolongerexistant, 30 June 2013 - 02:37 PM
nolongerexistant #1
Posted 30 June 2013 - 04:37 PM
Is there a way to do some precise error catching using pcall? For example with the Stargates mod and OpenPeripherals you can get errors like: Not enough Fuel, or Invalid Stargate ID, etc..

Is there a way to get the exact error it's throwing?

I've read BIT's Error Handling tutorials, but I don't think one of those examples is what I need.
0099 #2
Posted 30 June 2013 - 04:43 PM
If you want to get an error message, pcall returns it after "false" status
stat, mes = pcall(…)
if stat == false then
print("Error: "..mes)
if mes == "Invalid Stargate ID" then … end

else
print("no errors")
end
Engineer #3
Posted 30 June 2013 - 05:32 PM
Is there a way to do some precise error catching using pcall? For example with the Stargates mod and OpenPeripherals you can get errors like: Not enough Fuel, or Invalid Stargate ID, etc..

Is there a way to get the exact error it's throwing?

I've read BIT's Error Handling tutorials, but I don't think one of those examples is what I need.

TOBIT does have what you need. For a matter of fact, its the only way you could do it.. example:


local ok, err = pcall(
   function() 
     -- insert code
   end
) 
if not ok then
print("Got error: " .. err )
end

I dont know how stargate and OpenP give those errors. So it really depends
nolongerexistant #4
Posted 30 June 2013 - 06:05 PM
Is there a way to do some precise error catching using pcall? For example with the Stargates mod and OpenPeripherals you can get errors like: Not enough Fuel, or Invalid Stargate ID, etc..

Is there a way to get the exact error it's throwing?

I've read BIT's Error Handling tutorials, but I don't think one of those examples is what I need.

TOBIT does have what you need. For a matter of fact, its the only way you could do it.. example:


local ok, err = pcall(
   function()
	 -- insert code
   end
)
if not ok then
print("Got error: " .. err )
end

I dont know how stargate and OpenP give those errors. So it really depends

That did it, but is there some way you can remove the file name and line number too?
Engineer #5
Posted 01 July 2013 - 04:33 AM
Of course, that would be some string manipulation. This will not work for all cases:


local ok, err = pcall(
    function()
      --stubb
    end
) 
if not ok then
  print( err:gsub(".+:+.+:+[ ]", "") ) 
end

Im bad at patterns :D/>
nolongerexistant #6
Posted 01 July 2013 - 08:00 AM
Thanks Engineer, a great help as always :)/>
theoriginalbit #7
Posted 01 July 2013 - 09:04 AM
I believe in the final code I provided in my tutorial also showed you how to remove the file and line number information at the start. Although it is not as nice as Engineers solution.

Also if you're throwing any manual errors then using a level of 0 would also stop file and line numbers being shown.