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

Error Code API

Started by nateracecar5, 06 January 2014 - 03:50 PM
nateracecar5 #1
Posted 06 January 2014 - 04:50 PM
I made this API for myself because I needed a way to shrink one of my programs from about 50 lines, to about 20. Basically this API adds the ability to easily add error codes and messages, and lookup them up, native error parsing, or remove them with ease.

The abilities with this API include:
The ability to add error codes to a temporary table
The ability to lookup error codes; If the lookup fails, the api has a failsafe.
The ability to remove error codes from the table, incase you ever want to.
The ability to natively parse errors with pcall

The functions are:

ecAPI.addCode(code, message)
ecAPI.lookupCode(code)
ecAPI.removeCode(code)
ecAPI.parseError(func)

An example of this is, say, you're on a server which you have a big computercraft network hooked up. The network gives you an error code, and you can easily look it up on the computer, or on a separate computer.

The api is made for easy use. Just have 1 lookup function, and all you have to do is, at the beginning of the code add your error codes.

For instance, if you want to add the error code "1" and have it say "Your program has crashed", all you have to do is put

ecAPI.addCode("1","Your program has crashed")
at the beginning of your program.

An example program is:
Spoiler
local oldPull = os.pullEvent;
os.pullEvent = os.pullEventRaw;
os.loadAPI("ecAPI")
ecAPI.addCode("102", "Blank Ticket. Rewrite Ticket")
ecAPI.addCode("108", "Ticket File Exists, No Ticket Number Within. Rewrite Ticket")
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(colors.white)
term.setTextColor(colors.cyan)
term.clear()
write("Error code: ")
code = read()
if code == "edit codes" then
  shell.run("edit", "startup")
  os.reboot()
else
  ecAPI.lookupCode(code)
  sleep(3)
  os.reboot()
end
os.pullEvent = oldPull;

This program is from my train station I built as an experiment with random string generators (Don't ask…). I added error codes so that if something went wrong with the ticket inside the disk drive (Which I'm treating as the "ticket scanner") the computer would output an error. Then I could lookup the error, see what went on, and fix it easily.

You might think that since the API adds the codes to a table, once the computer is rebooted, the table will get removed. Well, at the beginning of every program, the computer re-inserts the code into the table. So it doesn't matter if the server get's restarted, or the computer get's shutdown, the codes will stay put. Plus, that means that no pesky temporary files are generated.

That's enough jabbering on my part, just download the api and use it for yourself!

pastebin: GT3JZBa6

pastebin get GT3JZBa6 ecapi

I really hope you all like it!
Edited on 07 January 2014 - 11:03 AM
robhol #2
Posted 06 January 2014 - 05:42 PM
What's it good for, though? Wouldn't it generally be easier to just output the error message?
nateracecar5 #3
Posted 06 January 2014 - 10:58 PM
I know it would be easier to do that, doing this:

if code == "12" then
  print("Crashed")
elseif code == "13" then
  print("Errored out")
elseif *blah blah blah*
Would be a lot more annoying then doing this:

write("Lookup error: ")
code = read()
ecAPI.lookupCode(code)

So yeah, just think of how much lines of code you could save. About as many lines as you have error codes. :P/>

With my lookupCode function, it will print the error, or print out a message saying there is no code.
The lookupCode function has a failsafe, which I explained in the forum post above.

So really all you need to do is put that line of code, and do anything you want afterwards. This includes:

os.reboot()
sleep(number)
print("")
shell.*function*

And anything else you want to have under that line of code.
Edited on 06 January 2014 - 09:59 PM
robhol #4
Posted 06 January 2014 - 11:43 PM
What I mean is, error("crashed") error("expected a Y") error("no materials") - which saves even more lines.
oeed #5
Posted 06 January 2014 - 11:48 PM
What happens if you add more code, changing the line numbers?
theoriginalbit #6
Posted 06 January 2014 - 11:49 PM
You should definitely look into parsing the error message to find out file names and line numbers :)/>
nateracecar5 #7
Posted 07 January 2014 - 01:53 AM
What happens if you add more code, changing the line numbers?

I don't know what you mean…

You should definitely look into parsing the error message to find out file names and line numbers :)/>

I don't know what you mean either XD


If you guys could explain what you mean, that would be great.

EDIT: Never mind, I know what Original means, now just trying to figure out what oeed means.
Edited on 07 January 2014 - 12:54 AM
oeed #8
Posted 07 January 2014 - 02:19 AM
What happens if you add more code, changing the line numbers?

I don't know what you mean…

You should definitely look into parsing the error message to find out file names and line numbers :)/>

I don't know what you mean either XD


If you guys could explain what you mean, that would be great.

EDIT: Never mind, I know what Original means, now just trying to figure out what oeed means.

Oh, don't worry. I was under the influence that the number at the beginning of the function was the line on which the error occurred. However, after having a deeper look this is obviously not the case.
nateracecar5 #9
Posted 07 January 2014 - 12:00 PM
You should definitely look into parsing the error message to find out file names and line numbers :)/>

Okay, I've got a way to parse the error with pcall, now I'm just wondering how I would scan the error code table, to see what to print. Do you think you could help me?
P.S. I've updated the paste so you can download it and look at the "parseError" function.
robhol #10
Posted 08 January 2014 - 12:30 AM
Aside from the fact that there's no valid reason to convert a legible error message into a number you have to look up (to get a legible error message and not a number you have to look up)…

As an exercise in coding, you could make an array with all possible errors and do a reverse lookup.
I really don't get the point of this "API" though - no offense, but really, what?

To put it bluntly: why do you think that this is useful?
Edited on 07 January 2014 - 11:30 PM
oeed #11
Posted 08 January 2014 - 02:10 AM
Aside from the fact that there's no valid reason to convert a legible error message into a number you have to look up (to get a legible error message and not a number you have to look up)…

As an exercise in coding, you could make an array with all possible errors and do a reverse lookup.
I really don't get the point of this "API" though - no offense, but really, what?

To put it bluntly: why do you think that this is useful?

I see what you mean. The best way would be to check for potential errors (e.g. If … == nil) then use error('lalala')
nateracecar5 #12
Posted 08 January 2014 - 05:47 PM
The only reason I made this was to shrink down some of my code I had, and thought it would be a good exercise in coding since I've never really used tables before.