Jump to content




pcall error catching

computer lua

  • You cannot reply to this topic
6 replies to this topic

#1 nolongerexistant

  • Validating
  • 201 posts
  • LocationNetherlands

Posted 30 June 2013 - 03: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.

#2 0099

  • Members
  • 52 posts

Posted 30 June 2013 - 03: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

#3 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 30 June 2013 - 04:32 PM

View PostSnakybo, on 30 June 2013 - 03:37 PM, said:

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

#4 nolongerexistant

  • Validating
  • 201 posts
  • LocationNetherlands

Posted 30 June 2013 - 05:05 PM

View PostEngineer, on 30 June 2013 - 04:32 PM, said:

View PostSnakybo, on 30 June 2013 - 03:37 PM, said:

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?

#5 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 01 July 2013 - 03: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

#6 nolongerexistant

  • Validating
  • 201 posts
  • LocationNetherlands

Posted 01 July 2013 - 07:00 AM

Thanks Engineer, a great help as always :)

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 01 July 2013 - 08: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users