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

Question For Creating Custom Errror Codes

Started by Termanater13, 10 August 2013 - 09:49 AM
Termanater13 #1
Posted 10 August 2013 - 11:49 AM
I know lua when it has a issue it will tell you the file that has the issue. Is there a way to tell the user where the error occurred in there code when they use user created APIs. I'm looking to use this to write errors codes in my API so I can keep the wrong actions from happening. I wnt this so I can tell the user somehing like whats below

"BE:test:34:wrong data type given"
BE = just being my code for Button Error
test = would be the file that caused the error
34 = the line the error is on
the last part would tell the user how it happened
Bubba #2
Posted 10 August 2013 - 01:12 PM
It's pretty simple actually. You can throw errors by using the error function.


local function thing(arg1, arg2)
  if not arg2 then
    error("You did not provide enough arguments!")
  end
end

thing()

This will throw the error: "filename:3: You did not provide enough arguments!"

If you want the line number to be the line that calls the function, use error("message", 2) instead. That would output "filename:7: You did not provide enough arguments!"
Engineer #3
Posted 10 August 2013 - 01:58 PM
To add to Bubba, you also have an error level of 0.
So, this for example:

local function thing(arg1, arg2)
  if not arg2 then
    error("You did not provide enough arguments!",0)
  end
end

thing()
It will just output, if on an advanced computer in red otherwise in just white: You did not provide enough arguments!
This is for example usefull if you want to notify the user if he did something wrong, it simply gets rid of the line number and stuff.
jay5476 #4
Posted 11 August 2013 - 05:34 PM
error will also stop your code, so just keep that in mind i better solution would be just to prompt them shortly saying …
Bubba #5
Posted 11 August 2013 - 06:27 PM
error will also stop your code, so just keep that in mind i better solution would be just to prompt them shortly saying …

The point is that he wants to notify them that they are using the API wrong. If they're using it wrong, the code should not keep running. Hence no, a better solution would not be just prompting them.
albrat #6
Posted 13 August 2013 - 01:40 PM
– Edit :: My information is already posted… Oops.

Bubba posted a better way of doing this than my example.