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

Use pcall() to handle errors

Started by Jummit, 05 October 2017 - 11:17 AM
Jummit #1
Posted 05 October 2017 - 01:17 PM
Did you had the problem that the error line wasn't visible because of you're buffering?
Or did you want to write the error of a program in a log file?


You can easily do this by using the pcall() function.
Here is my simple program from which i want to to log the errors from.

buffer = window.create(term.current(), 1, 1, term.getSize())
local oldterm = term.redirect(buffer)

buffer.setVisible(false)
error("test")
buffer.setVisible(true)
You see the 'error("test")'? Normally it would show a red error on the terminal. But we redirected the terminal to a buffer which doesn't get drawn because we set it invisible. So we will never see the error.

How we can use the pcall function to write the error(if there is one) on the screen:

buffer = window.create(term.current(), 1, 1, term.getSize())
local oldterm = term.redirect(buffer)
function main()
  buffer.setVisible(false)
  error("test")
  buffer.setVisible(true)
end

no_error, message = pcall(main)
term.redirect(oldterm)
shell.run("clear")
if not no_error then
  print(message)
end
The pcall function needs a function. It runs it and returns if it was successful or not and the error if there was one.
So we create a function main, which holds our program and run pcall with main as argument.
Now we get no_error, a variable which is false if the program failed and true if there was no error and message, which is the error.
Now we only need to check if there is an error, and if it is so, we print it. You can of course do what you want with the error, write it to a file or make a nice UI and print it on the crash.

Hope you found this helpful and learned something!

Further reading:
Lua documentation of pcall and more: https://www.lua.org/....html#pdf-pcall
Buffering with Computercraft if you don't know how it works: http://www.computerc...ki/Window_(API)
Edited on 05 October 2017 - 11:25 AM
OrdinaryCassetteNerd #2
Posted 04 December 2017 - 05:59 PM
LOL, not only useful for buffer users. Also useful for GUI OSes. That stuff aside, great work.
Lupus590 #3
Posted 05 December 2017 - 09:44 PM
LOL, not only useful for buffer users. Also useful for GUI OSes. That stuff aside, great work.

GUIs also buffer. An OS would want to use it to protect itself from erroring programs. Someone may think of other uses.
OrdinaryCassetteNerd #4
Posted 10 December 2017 - 07:06 PM
LOL, not only useful for buffer users. Also useful for GUI OSes. That stuff aside, great work.
GUIs also buffer. An OS would want to use it to protect itself from erroring programs. Someone may think of other uses.

It would be great if I knew how to use a buffer 'cause I also plan on creating a GUI OS.
Lupus590 #5
Posted 11 December 2017 - 06:19 PM
It would be great if I knew how to use a buffer 'cause I also plan on creating a GUI OS.

A graphics buffer in CC is basically a terminal object which doesn't render to the screen (or what ever terminal object it redirects to) until told to do so, thereby buffering all it's contents before rendering it all at once.

The window API is a very basic buffer, you may want to look at how other buffer APIs work.