I have a lua function which takes multiple parameters and returns as many values as there are parameters. Each return value corresponds to a parameter. To illustrate, consider a function which reads the value for a key/value pair from a database:
val1, val2, val3 = my_function("key1", "key2", "key3");
val1 = my_function("key1");
What is the best way to return an error from my_function? (e.g. if a supplied “key” is invalid)
I understand one way is to return two values on error, nil and an error string. Is this the best approach? For example:
val1, val2, val3 = my_function("key1", "key2", "key3");
if val1 then
-- Use val1, val2, and val3.
else
print("ERROR: " .. val2);
end
Edit
Some additional points:
- The lua script is being executed from within a C program using
lua_pcall(). - The C program must not abort if the script fails.
my_function()is implemented in C.- When
my_function()fails, it should also return an error code (or message) indicating the reason it failed.
The standard way of throwing errors in Lua is via the error function (manual, api) or via assert (which internally uses
erroranyway).Since your function is in C, you should be calling lua_error inside it, to gain the same effect.
But keep in mind that your function now is “insecure”. If unmodified, the following code will do the equivalent of “throwing an exception” and thus halting the program, if key1, key2 or key3 are “erroneous”:
Sometimes it’s ok to let the program “just crash” if the inputs are wrong. Depending on your setup, the user will get a message with the last error, and a stack trace, or something along those lines.
If “letting the program crash” is not an option, you can do the equivalent of surrounding it with a “try-catch” block by using the pcall function, and adding a new variable called
ok:Notice that you don’t have to put
pcallexactly on top ofmy_function. As with exceptions, the error recuperation can happen higher in the call chain: in the function callingmy_function, or the function calling that one, etc. For example, if you callmy_functionfrom a function calledparent, and parent from one calledgrandParent, you can do this: