Jump to content




[Question] Custom error function?

lua

5 replies to this topic

#1 toxicwolf

  • Members
  • 201 posts
  • LocationUnited Kingdom

Posted 25 September 2012 - 09: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


#2 Noodle

  • Members
  • 989 posts
  • LocationSometime.

Posted 25 September 2012 - 09:59 PM

View Posttoxicwolf, on 25 September 2012 - 09:53 PM, said:

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)

#3 toxicwolf

  • Members
  • 201 posts
  • LocationUnited Kingdom

Posted 25 September 2012 - 10: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.

#4 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 25 September 2012 - 10:06 PM

It's not possible. That information is added java side.

#5 toxicwolf

  • Members
  • 201 posts
  • LocationUnited Kingdom

Posted 25 September 2012 - 10:08 PM

View PostMysticT, on 25 September 2012 - 10:06 PM, said:

It's not possible. That information is added java side.
Okay, thank you! At least I know.

#6 GopherAtl

  • Members
  • 888 posts

Posted 25 September 2012 - 10: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()





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users