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

[Question] Custom error function?

Started by toxicwolf, 25 September 2012 - 08:53 PM
toxicwolf #1
Posted 25 September 2012 - 10:53 PM
I am trying to modify the error() function to print the error location and message using a printer when called. So far, I can get it to print the error message, but I was wondering how the original error function gets it's location information so I could replicate it somehow.

Here's what I have so far (added to the bios.lua file):
local nativeError = error
function error(err, level)
local printer = nil
for _, sSide in pairs(rs.getSides()) do
  if peripheral.isPresent(sSide) and peripheral.getType(sSide) == "printer" then
   printer = peripheral.wrap(sSide)
   break
  end
end
if printer then
  if printer.newPage() then
   printer.setPageTitle("WolfOS Error Log")
   printer.write(err)
   printer.endPage()
   print("Error log printed.")
  end
end
nativeError(err, level)
end
Noodle #2
Posted 25 September 2012 - 10:59 PM
I am trying to modify the error() function to print the error location and message using a printer when called. So far, I can get it to print the error message, but I was wondering how the original error function gets it's location information so I could replicate it somehow.

Here's what I have so far (added to the bios.lua file):
local nativeError = error
function error(err, level)
local printer = nil
for _, sSide in pairs(rs.getSides()) do
  if peripheral.isPresent(sSide) and peripheral.getType(sSide) == "printer" then
   printer = peripheral.wrap(sSide)
   break
  end
end
if printer then
  if printer.newPage() then
   printer.setPageTitle("WolfOS Error Log")
   printer.write(err)
   printer.endPage()
   print("Error log printed.")
  end
end
nativeError(err, level)
end
I don't think this is possible..
All you can do is have it go to a function like
cust_err(err, level)
toxicwolf #3
Posted 25 September 2012 - 11:01 PM
The code I posted works, I have tested it in-game. It's just locating where the error occurs to print that as well which I can't do.
MysticT #4
Posted 25 September 2012 - 11:06 PM
It's not possible. That information is added java side.
toxicwolf #5
Posted 25 September 2012 - 11:08 PM
It's not possible. That information is added java side.
Okay, thank you! At least I know.
GopherAtl #6
Posted 25 September 2012 - 11:30 PM
only way I know to get, in code, the line and error message is if you call whatever generated the error with pcall(), ex.


--gives error if p1 and p2 are mismatched types
function myFunction(p1,p2)
  return p1>p2
end

--use properly
ok, result=pcall(myFunction,0,5)
--ok=true (no error), result=false, the returned value of the function

--generate an error
ok, err=pcall(myFunction,"a",6)

--nothing will have printed to screen, instead error will be captured to "err" and ok will be false.

overriding error is possible, but it'll only replace explicit calls to error() in lua. Most are thrown in java, and so will ignore your error() function replacement.

:edit: I should note, this will NOT work calling shell.run(), because shell.run already uses pcall() to catch the error, then prints it to the screen. You can use it with os.run, though, or implement your own version of shell.run()