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

CCatch - Error catching and handling made easy

Started by NDFJay, 16 February 2013 - 04:56 AM
NDFJay #1
Posted 16 February 2013 - 05:56 AM


Ever wanted to make a real working BSoD? or just catch and resolve errors before your program crash's? or maybe just display a custom screen if your program crash's calling the user an idiot for messing with the code?


Well now you can! easily!

This API is made to make it easier to catch and resolve errors and display custom error screens!


–Note! This API was inspired by [member='TheOriginalBIT'] 's Handling and BSoD tutorial found here

Features:
  • Backwards compatible CC1.3+
  • Functions for both Advanced Computers and Standard Computers
  • Pre-designed error screens
  • Custom error screens
  • Custom crash handle functions
  • Colour and non colour support
  • Save error log to custom directory function
  • C# style error catching function for C# style main functions
Usage:
SpoilerThe Functions:

Usage and Examples:

For this I will use the following code as an example program, the uses will be direct to this code as if I was calling the function in this program


os.loadAPI("CCrash")

local tArgs = {...}
term.clear()
local w,h = term.getSize()
local col = "a"

local main = function()
term.setBackgroundColor(col)
end

local tmpErr = {
"Custom Crash";
" ";
"Error Code";
":error: ";
" ";
"Please report this code to the dev if this is";
"no fault of your own";
" ";
" ";
"Please do refrain from modifying the base code!"
}

res = function()
col = 1
main()
end

main()

Now looking at that code you can see that it will error on line 8 with the error: 8 : Expected number, this is fine since I want it to error

You can also see that the main code is wrapped in a function titled "main", this is needed since the API can only check functions for errors, this is due to the pcall function that is used so just wrap you main code in a function, the function can be titled anything you want, just remember it.

Next you will see a table called tmpErr, this will be used in the Custom crash screens and is shown in here for example and demonstration purposes.
You will also see ":error:" in the table, this gets replaced with the error message and is needed to print the error message

Next is the function titled "res" this is a crash resolve function, this will be used to try recover an error by using a different function or different information.

Now that thats out of the way lets explain the usage of the functions, firstly lets explain the args:

func = The function you want to catch error in, your main function/main code wrapped in a function
resolvefunc = The function used to resolve an error without showing an error
align = text alignment on the crash screen - options("left","center")
bcol = Background colour in binary bits example - (white = 1, gray = 128, lightblue = 8)
tcol = Text colour in binary bits
t = Table to use for custom crash screens (contains custom text as shown above in tmpErr)
x = xPos to print "error log saved to" text on left align
y = yPos to print "error log saved to" text on left and center align
sDir = Directory to save the error log to
sPath = The name of the error log
argc = C# style arg count for main function
argv = C# stye args for main function
"(error)" = Used in custom tables to tell the API where to print the error message
Right now on to how to use it:

First you need to wrap your main code/loop in a function as shown above then add one of the CCrash functions to the end of the code.


Advanced Computers(CC1.4+)

SpoilerQuick pre-made crash screen

CCatch.A_crash(func,align,bcol,tcol)

Custom crash screen



CCatch.A_custCrash(func,align,bcol,tcol,t)

Custom crash resolve



CCatch.A_resolveCrash(func,resolvefunc)


Custom save error log


CCatch.A_saveCrash(func,align,bcol,tcol,t,x,y,sDir,sPath)


Custom crash for C# style functions


CCatch.A_c_Crash(func,#{...},{...},align,bcol,tcol,t)

Custom save error log + crash screen for C# style functions


CCatch.A_save_c_Crash(func,align,bcol,tcol,t,x,y,sDir,sPath,#{...},{...})

Normal Computers(CC1.3+)

SpoilerQuick pre-made crash screen

CCatch.B_crash(func,align)

Custom crash screen



CCatch.B_custCrash(func,align,t)

Custom crash resolve



CCatch.B_resolveCrash(func,resolvefunc)


Custom save error log


CCatch.B_saveCrash(func,align,t,x,y,sDir,sPath)


Custom crash for C# style functions


CCatch.B_c_Crash(func,#{...},{...},align,t)

Custom save error log + crash screen for C# style functions


CCatch.A_save_c_Crash(func,align,t,x,y,sDir,sPath,#{...},{...})

Coming Soon:
  • Custom logo printing and positioning
  • Email error report
Download:
bjornir90 #2
Posted 16 February 2013 - 06:42 AM
Wow what a logo ! :P/> Anyways looks really great ! You are really good, I think you have experienced coding before cc, no ?
NDFJay #3
Posted 16 February 2013 - 06:50 AM
Wow what a logo ! :P/> Anyways looks really great ! You are really good, I think you have experienced coding before cc, no ?

Haha, I may have done Graphics design in the past :P/> haha and thanks :)/> I have dont some coding before CC yes, a little bit of C++, mainly web platforms though
bjornir90 #4
Posted 16 February 2013 - 06:55 AM
Haha, I may have done Graphics design in the past :P/>/> haha and thanks :)/>/> I have dont some coding before CC yes, a little bit of C++, mainly web platforms though
You should be learning really fast so :)/>
remiX #5
Posted 16 February 2013 - 07:10 AM
Quite useful :3 Nice Api :P/>/>

EDIT:

I'm busy adding this to my program and it's kind of not working :<

I'm using the A_saveCrash function for my program with a table looking like this:
local t_ErrorLog = {
	"Oh No! It looks like";
	"ComputerCraft YouTube has crashed!";
	"";
	"Error Code:";
	"(error)";
	"";
	"Please report this error code to remiX";
	"if this is no fault of your own,";
	"otherwise state what you have changed."
}

And I'm calling it like this:
CCrash.A_saveCrash( controlPanel, "center", colours.red, colours.white, t_ErrorLog, 1, screenY-1, sourceFolder, 'errorLog.txt' )

And the result is shown in the attachment.
It looks like on this line:
"Please report this error code to remiX";
it's also replacing 'error' but I don't understand why because it should look for "(error)"

Also: The error file that it's supposed to write the error to doesn't get created.
NDFJay #6
Posted 19 February 2013 - 09:33 AM
–snip–

Sorry for the late reply… been at work all day and havent been on the internet all weekend…

the issue was a syntactical error on my part… fixed it now new download link

pastebin get Q07qDtje CCatch

or pastebin.com/Q07qDtje

in your table replace "(error)" with ":error:", I tested it with your table and syntax and it works perfectly
theoriginalbit #7
Posted 19 February 2013 - 01:45 PM
you didn't update the link for the pastebin in OP properly. You just updated the text ;)/> :P/>

Oh btw looks good :)/>

one suggestion, this is a better way for centre printing text

term.setCursorPos( math.floor( screenWidth / 2 - #msg / 2 + (#msg % 2 and 0 or 1) ), y )
this means that the text is put in the middle when even, and 1 pixel to the right when odd, both then appear to be perfectly in the middle ;)/> … I've found this works because of how computer have an odd amount of pixels.
NDFJay #8
Posted 19 February 2013 - 06:34 PM
you didn't update the link for the pastebin in OP properly. You just updated the text ;)/> :P/>

Oh btw looks good :)/>

one suggestion, this is a better way for centre printing text

term.setCursorPos( math.floor( screenWidth / 2 - #msg / 2 + (#msg % 2 and 0 or 1) ), y )
this means that the text is put in the middle when even, and 1 pixel to the right when odd, both then appear to be perfectly in the middle ;)/> … I've found this works because of how computer have an odd amount of pixels.

Damn I didnt even realise! haha and thanks for the tip :)/> I shall add it in ^_^/> when I get back from work that is lol
rhysjack7 #9
Posted 15 April 2013 - 07:16 AM
Please Can I Use It As A Bootloader For CraftdowsOS
NDFJay #10
Posted 15 April 2013 - 07:43 AM
Please Can I Use It As A Bootloader For CraftdowsOS

This isnt a bootloader, if you want a bootloader look for NBOOT+ if you want to use this for crash handling within the OS the by all means, go for it