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

Display Error in another Program

Started by Chickenbreadlp, 01 May 2014 - 03:02 PM
Chickenbreadlp #1
Posted 01 May 2014 - 05:02 PM
I'm working on an OS and i want to Display Errors that were caused by a program in another Program. How do i dow this?
My Example:
[indent=1]Program 1 Is crashed because an 'end' was missing. In a shell The following error would be displayed:[/indent]
[indent=1]

bios:366: [string "PROGRAMNAME"]:LINE: 'end' expected (to close 'CAUSE' at line LINE)
[/indent]
[indent=1]Bluescreen is automaticly displayd which should shows:

ERROR!!! Crash Code:
LINE: 'end' expectet (to close 'CAUSE' at line LINE)
[/indent]
[indent=1](or something like that)[/indent]
Can i do this somehow?
Edited on 01 May 2014 - 03:04 PM
Bomb Bloke #2
Posted 01 May 2014 - 05:08 PM
pcall() sounds like what you're after.
Chickenbreadlp #3
Posted 01 May 2014 - 06:47 PM
Either i'm too dumb to use it or I'm using pcall() wrong… my OS uses multiple programs. I'm tested the following code in my Program:

local status = pcall(function () shell.run("ApfelOS/aLogin.adf") end)
print(status)
(please note, that this is exactly the same code that i tested)
but if an error appears or not, it says every time true
Edited on 01 May 2014 - 04:47 PM
Lyqyd #4
Posted 01 May 2014 - 07:01 PM
That's because os.run is already pcall-ing the program. If it errors, it prints the error and exits cleanly. You'll need to write a modified version of os.run and use it instead of calling shell.run.
Chickenbreadlp #5
Posted 01 May 2014 - 07:11 PM
I'm a little bit dumb about that. How do i do this? (normally i only need an example)
Bomb Bloke #6
Posted 02 May 2014 - 02:13 AM
The source is found in bios.lua:

function os.run( _tEnv, _sPath, ... )
    local tArgs = { ... }
    local fnFile, err = loadfile( _sPath )
    if fnFile then
        local tEnv = _tEnv
        --setmetatable( tEnv, { __index = function(t,k) return _G[k] end } )
        setmetatable( tEnv, { __index = _G } )
        setfenv( fnFile, tEnv )
        local ok, err = pcall( function()
            fnFile( unpack( tArgs ) )
        end )
        if not ok then
            if err and err ~= "" then
                printError( err )
            end
            return false
        end
        return true
    end
    if err and err ~= "" then
        printError( err )
    end
    return false
end
Chickenbreadlp #7
Posted 02 May 2014 - 11:32 AM
I think, i'm too dumb for that, because now it either says:

window:57:expectet Number
Press any key
or it says:

bios:attempt to call nil
(i have forgotten the number at the second one)
I need to run a program which has some color variables in it to have something like a theme.
Bomb Bloke #8
Posted 02 May 2014 - 04:53 PM
Sorry, can't really comment on that without seeing the script(s) that led to those errors.
apemanzilla #9
Posted 02 May 2014 - 05:56 PM
Try this.

local status, error = pcall(function() dofile("filename") end)
Chickenbreadlp #10
Posted 02 May 2014 - 09:05 PM
Now my code is ready to publish, because i must check if there is anything that i don't want to show so here it is:
1st Program:
Spoiler

function run( _tEnv, _sPath, ... )
	local tArgs = { ... }
	local fnFile, err = loadfile( _sPath )
	if fnFile then
		local tEnv = _tEnv
		--setmetatable( tEnv, { __index = function(t,k) return _G[k] end } )
		setmetatable( tEnv, { __index = _G } )
		setfenv( fnFile, tEnv )
		local ok, err = pcall( function()
			fnFile( unpack( tArgs ) )
		end )
		if not ok then
			if err and err ~= "" then
				os.run( {}, "ApfelOS/..colortheme.ac")
	x = 0
	CCver = os.version()
	if fs.exists("ApfelOS/..Session/login") then
	 fs.delete("ApfelOS/..Session/login")
	end
	local loop = "on"
	term.setBackgroundColor(error)
	term.clear()
	term.setTextColor(error)
	term.setBackgroundColor(errortxt)
	term.setCursorPos(22,4)
	print(" Error ")
	term.setBackgroundColor(error)
	term.setTextColor(errortxt)
	print(" ")
	print("	 An Error has occured and the  OS has been	 ")
	print(" shutdown. To power off your PC press CTRL + S for ")
	print("					 1 second.					 ")
	print(" ")
	print(" ")
	write("OStag: ")
	print(OStag)
	write("OS Version: ")
	print(version)
	write("OS Modding: ")
	if modding == "true" then
	 print("Enabled")
	elseif modding == "false" then
	 print("Disabled")
	else
	 print("Can't indentify modding status")
	end
	write("OS Starts: ")
	print(starts)
	write("Shell Version: ")
	print(CCver)
	write("Error Code: ")
	print( err )
	while true do
	local event, button, X, Y = os.pullEventRaw("mouse_click")
	XY = X..","..Y

	 if x == 10 then
	  os.shutdown()
	 break
	 end
	end
			end
			return false
		end
		return true
	end
	if err and err ~= "" then
		bug = err
	end
	return false
end
2nd Program:
Spoiler

-- All Colors
-- 1	 - White
-- 2	 - Orange
-- 4	 - pink
-- 8	 - light blue
-- 16	- Yellow
-- 32	- light green
-- 64	- rosa
-- 128   - grey
-- 256   - light grey
-- 512   - cyan
-- 1024  - violette
-- 2048  - blue
-- 4096  - braun
-- 8192  - green
-- 16384 - red
-- 32768 - black
function Loading()
sleep(0.2)
write(" ")
sleep(0.1)
write(" ")
sleep(0.1)
write(" ")
sleep(0.1)
write(" ")
sleep(0.1)
write(" ")
sleep(0.1)
write(" ")
sleep(0.1)
write(" ")
end
function menue(m)
n=1
k=#m
while true do
term.clear()
term.setCursorPos(1,1)
term.setTextColor(16)
print("ApfelBoot - Pro Bootloader")
term.setCursorPos(1,3)
for i=1, k, 1 do
if i==n then print(i, " ["..m[i].."]") else print(i, "  "..m[i].." ") end
end
print("")
print("<Please select your Operating System>")
a, b= os.pullEventRaw()
if a == "key" then
if b==200 and n>1 then n=n-1 end
if b==208 and n<=k then n=n+1 end
if b==28 then break end
end
end
term.clear() term.setCursorPos(1,1)
return n
end
term.setBackgroundColor(32768)
term.clear()
term.setCursorPos(1,1)
if fs.exists("ApfelOS/..Session/login") then
fs.delete("ApfelOS/..Session/login")
end
if fs.exists("ApfelOS/newversion") then
fs.delete("ApfelOS/newversion")
end
term.clear()
term.setTextColor(16)
term.setCursorPos(1,1)
textutils.slowPrint("ApfelBoot - Pro Bootloader")
sleep(0,9)
term.clear()
sleep(0,1)
local options={
"ApfelOS",
"ApfelShell",
"Shutdown"
}
local n=menue(options)
print(n)
if n == 1 then
local file = fs.open("ApfelOS/..stcount.adf", "r")
local fileData = {}
local line = file.readLine()
repeat
table.insert(fileData,line)
line = file.readLine()
until line == nil
file.close()
starts = fileData[1]
term.clear()
term.setCursorPos(1,1)
term.setTextColor(16)
print("				  ApfelOS 1.0 Pro				  ")
print(" ")
write("	")
term.setBackgroundColor(2)
print("										   ")
term.setBackgroundColor(32768)
write("	")
term.setBackgroundColor(2)
print("										   ")
term.setBackgroundColor(32768)
write("	")
term.setBackgroundColor(2)
print("										   ")
term.setBackgroundColor(32768)
print(" ")
Logo = paintutils.loadImage("ApfelOS/Logo.nfp")
Background = paintutils.loadImage("ApfelOS/background.nfp")
os.loadAPI("ApfelOS/API/aoBasic")
aoBasic.run( {}, "ApfelOS/API/apfelOS")
freeSpace = fs.getFreeSpace("/")
spacer = " "
format = "B "
CCver = os.version()
paintutils.drawImage(Logo,18,6)
term.setCursorPos(6,4)
term.setBackgroundColor(16)
textutils.slowPrint("										 ")
sleep(0.3)
if starts == "0" then
starts = starts + 1
local file = fs.open("ApfelOS/..stcount.adf", "w")
file.write(starts)
file.close()
theme = "sky"
apfelOS.run("ApfelOS/..firststart.adf")
error = colors.blue
errortxt = colors.white
else
starts = starts + 1
local file = fs.open("ApfelOS/..stcount.adf", "w")
file.write(starts)
file.close()
shell.run("ApfelOS/..colortheme.ac")
aoBasic.run( {}, "ApfelOS/aLogin.adf")
end
os.shutdown()
elseif n == 2 then
term.clear()
print("			   ApfelShell is loading			   ")
sleep(0.6)
shell.run("ApfelOS/shell.adf")
elseif n == 3 then
term.clear()
term.setCursorPos(1,1)
print("ApfelBoot - Pro Bootloader")
print(" ")
print("1  ApfelOS")
print("2  ApfelShell")
print("3 [Shutdown]")
print(" ")
print("<Please select your Operating System>")
sleep(0.4)
os.shutdown()
else
term.clear()
term.setCursorPos(1,1)
term.setTextColor(16)
print("Sorry, but this is a known Bug, so please don't")
print(" report it a second time.")
sleep(5)
os.reboot()
end
3rd program:
Spoiler

shell.run("ApfelOS/..colortheme.ac")
term.setBackgroundColor(body)
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(tbar)
term.setTextColor(tbartxt)
print("												  ")
term.setCursorPos(1,1)
write("ApfelOS 1.0 Pro ")
print(version)
term.setCursorPos(46,1)
term.setTextColor(prog)
print("Login ")
term.setTextColor(mbartxt)
term.setBackgroundColor(mbar)
print("<No Message available>							 ")
term.setTextColor(wico)
term.setCursorPos(11,5)
term.setBackgroundColor(wicotxt)
print("		   Login			")
term.setBackgroundColor(wico)
term.setTextColor(wicotxt)
term.setCursorPos(11,6)
print("							")
term.setCursorPos(11,7)
print("							")
term.setCursorPos(11,8)
print("							")
term.setCursorPos(11,9)
print("							")
term.setCursorPos(11,10)
print("							")
term.setCursorPos(11,11)
print("							")
term.setCursorPos(12,7)
write("Username: ")
term.setCursorPos(12,9)
print("Password: ")
term.setCursorPos(22,7)
UserName = read()
term.setCursorPos(22,9)
UserPass = read("*")
if UserPass == "" then
shell.run("ApfelOS/aLogin.adf")
elseif UserName == "" then
shell.run("ApfelOS/aLogin.adf")
elseif UserName == "Reboot" then
os.reboot()
elseif UserName == "Shutdown" then
os.shutdown()
else

if fs.exists("ApfelOS/..Benutzer/"..UserName.."/"..UserPass.."") then
  if fs.exists("ApfelOS/..benutzer/"..UserName.."/"..UserPass.."/admin") then
   admin = true
  else
   admin = false
  end
  fs.makeDir("ApfelOS/..Session/login")
  term.setBackgroundColor(body)
  term.clear()
  term.setTextColor(tbartxt)
  term.setCursorPos(1,1)
  term.setBackgroundColor(tbar)
  print("												   ")
  term.setCursorPos(1,1)
  write("ApfelOS 1.0 Pro ")
  print(version)
  term.setBackgroundColor(mbar)
  term.setTextColor(mbartxt)
  print("<Please wait!>									 ")
  term.setBackgroundColor(body)
  term.setTextColor(bodytxt)
  term.setCursorPos(21,7)
  print("Welcome |")
  sleep(0.1)
  term.setCursorPos(21,7)
  print("Welcome /")
  sleep(0.1)
  term.setCursorPos(21,7)
  print("Welcome -")
  sleep(0.1)
  term.setCursorPos(21,7)
  print("Welcome |")
  sleep(0.1)
  term.setCursorPos(21,7)
  print("Welcome /")
  sleep(0.1)
  term.setCursorPos(21,7)
  print("Welcome -")
  sleep(0.1)
  term.setCursorPos(21,7)
  print("Welcome |")
  sleep(0.1)
  term.setCursorPos(21,7)
  print("Welcome /")
  sleep(0.1)
  term.setCursorPos(21,7)
  print("Welcome -")
  sleep(0.1)
  term.setCursorPos(21,7)
  print("Welcome |")
  sleep(0.1)
  term.setCursorPos(21,7)
  print("Welcome /")
  sleep(0.1)
  term.setCursorPos(21,7)
  print("Welcome -")
  sleep(0.1)
  term.setCursorPos(21,7)
  print("Welcome |")
  sleep(0.1)
  term.setCursorPos(21,7)
  print("Welcome /")
  sleep(0.1)
  term.setCursorPos(21,7)
  print("Welcome -")
  sleep(0.1)
else
  term.setCursorPos(12,10)
  term.setTextColor(16384)
  print("Wrong Username or Password")
  term.setTextColor(1)
  sleep(1)
  term.setBackgroundColor(8)
  term.clear()
  term.setCursorPos(1,1)
  shell.run("ApfelOS/aLogin.adf")
end
end
please note, that there are the original path's in this code. you have to change them to get it work
Engineer #11
Posted 03 May 2014 - 02:56 PM
Try this.

local status, error = pcall(function() dofile("filename") end)
Why dont you use the proper way of calling pcall, it has more arguments than one. Anonymous functions should only be used if you want to execute a chunk of code rather than just one function call. The proper of calling pcall in this matter is:

local status, err = pcall( dofile, "filename" )
And don't overwrite the global error function in your code, it can lead to unexpected errors :P/>