Lua/Errors

Lua modules based on the Scribunto/Lua extension are stored in resource pages using the Module: namespace. Each module uses a table to hold functions and variables, and that containing table is returned at the end of the module code. This lesson will show you how to troubleshoot script errors and handle run-time errors in your scripts.

Prerequisites
This lesson assumes you have already completed the Tables lesson.

Create a Lua Script with Errors and Error Handling
To create a Lua script with errors and error handling: ""
 * 1) Navigate to Module:Sandbox.
 * 2) Clear all existing code.
 * It's a sandbox. Everyone is free to play in the sandbox.  But if you find another user is actively editing the sandbox at the same time, you may also use Module:Sandbox/Username, where Username is your Wikiversity username.
 * 1) Add the following code and save the page:

Test Your Lua Script
To test your Lua script:
 * 1) Navigate to either the Module_talk:Sandbox page, the Sandbox page, or your own user or sandbox page.
 * 2) Add the following code and save the page:
 * Reciprocal 1


 * Reciprocal 2


 * Reciprocal 3


 * Reciprocal 4

The result should be similar to:
 * Reciprocal 1
 * inf
 * 0.5
 * inf
 * 0.5


 * Reciprocal 2
 * 0.5
 * 0.5
 * 0.5
 * 0.5


 * Reciprocal 3
 * 0.5
 * 0.5
 * 0.5
 * 0.5


 * Reciprocal 4
 * Error: Value must exist, must be numeric, and not zero.
 * Error: Value must exist, must be numeric, and not zero.
 * Error: Value must exist, must be numeric, and not zero.
 * 0.5

Understand Your Lua Script
To understand your Lua script  function: To understand your Lua script  function: To understand your Lua script  function:
 * 1)   declares a local function named   that accepts a single parameter , which is the value whose reciprocal will be returned.
 * 2)   returns the reciprocal of value.
 * 3)   ends the function.
 * 1)   declares a function named   that accepts a single parameter , which is the object used to access parameters passed from #invoke.
 * 2)   defines a local variable named   and assigns the value of the first frame argument (parameter) passed with #invoke.
 * 3)   calls the   function, passing the variable   and returns the result.
 * 4)   ends the function.
 * 1)   declares a local function named   that accepts a single parameter , which is the value whose reciprocal will be returned.
 * 2)   creates a conditional code block and tests to see if   is  .  If it is, an error is generated.
 * 3)   generates an error with the given literal string as the error statement.  Using   allows the script writer to determine the text of the error message that is returned to the calling function or wiki page that invoked the function.
 * When an error is generated, execution immediately returns to the calling function. Any additional code in the same function that comes after the error is not processed.
 * 1)   creates a conditional code block and tests to see if   is numeric.  If it is not,   returns   and an error is generated.
 * 2)   creates a conditional code block and tests to see if   is  .  If it is, an error is generated.
 * 3)   returns the reciprocal of value.
 * 4)   ends the function.

To understand your Lua script  function:
 * 1)   and the following code declares a function named   that accepts a single parameter , which is the object used to access parameters passed from #invoke.
 * 2)   defines a local variable named   and assigns the value of the first frame argument (parameter) passed with #invoke.
 * 3)   calls the   function, passing the variable   and returns the result.
 * 4)   ends the function.

To understand your Lua script  function:
 * 1)   declares a local function named   that accepts a single parameter , which is the value whose reciprocal will be returned.
 * 2)   creates a self-contained conditional code block and tests to see if   is    If it is, an error is generated.
 * It is best practice to use  to document and test any assumptions that are made regarding passed parameters.
 * 1)   creates a self-contained conditional code block and tests to see if   is numeric.  If it is not,   returns   and an error is generated.
 * 2)   creates a conditional code block and tests to see if   is  .  If it is, an error is generated.
 * 3)   returns the reciprocal of value.
 * 4)   ends the function.

To understand your Lua script  function:
 * 1)   and the following code declares a function named   that accepts a single parameter , which is the object used to access parameters passed from #invoke.
 * 2)   defines a local variable named   and assigns the value of the first frame argument (parameter) passed with #invoke.
 * 3)   calls the   function, passing the variable   and returns the result.
 * 4)   ends the function.

To understand your Lua script  function:
 * 1)   and the following code declares a function named   that accepts a single parameter , which is the object used to access parameters passed from #invoke.
 * 2)   defines a local variable named   and assigns the value of the first frame argument (parameter) passed with #invoke.
 * 3)   creates a conditional code block that calls the   function, passing   and storing the result.  If no error occurs,   is returned.  If an error occurs, a literal string is returned instead.
 * 4)   uses the pcall (protected call) function to call a function and catch any errors that occur.
 * 5)   creates a self-contained anonymous function that executes the code in ... as a function call.
 * The anonymous function is necessary to save the result of the  function while also using   to catch any errors that occur.
 * 1)   ends the function.

Conclusion
Congratulations! You've now created, tested, and understood a Lua script with error handling. Continue on to the ../Math Library/ lesson or return to the main Lua page to learn about other Lua code libraries.