developer_center:developer_editor:script:miniapi_tutorialerrorhandling

Script Error handling

How To handle errors in scripts

By default, lua already provides us with functions to handle errors but, mini world has already some measures in place, especially with the provided methods for all the classes so when using the game API you won't encounter as many errors as you might encounter when using standalone lua

LUA Built-in error handling functions

Pcall

  • This method stands for Protected Call and it is commonly used in Lua programming. It is pretty similar to try-catch statements in other programming languages, executing a block of code in a protected way, the pcall function takes two parameters: a function to be executed and its arguments. The purpose of pcall is to catch any errors that may occur during the execution of the provided function. If an error occurs, pcall returns false along with an error message; otherwise, it returns true and the results of the function.

We can use the pcall function like this:

  • sucess, result = pcall(PossibleErrorFunc, argsToFunc, …)

Use example:

-- Function that may produce an error
function divide(a, b)
    if b == 0 then
        error("Division by zero")
    end
    return a / b
end

-- Using pcall to protect the call function
local success, result = pcall(divide, 10, 2)

--If no errors encountered then else
if success then
    print("Result:", result)
else
    print("Error:", result)  -- This will print the error message if an error occurs
end

If there is any error when the function was executed the “result” (2nd variable) will give the error message, if there wansn't any error then the function will give what was returned in our function, and the sucess variable will be true if there are no errors

Xpcall

This method stands for External Protected call and as the name implies it allows the execution of a function in a protected way but it also includes an error handling function that can be specified to handle errors in a more custom form

We can use the Xpcall method this way:

  • success, result = xpcall(possibleErrorFunc, errorHandlerFunc, ArgsToFunc, …)

Use example:

-- Function that may produce an error
function divide(a, b)
    if b == 0 then
        error("Division by zero")
    end
    return a / b
end

-- Error handling function
function errorHandler(err)
    return "An error occurred: " .. err
end

-- Using xpcall to protect the function calling
local success, result = xpcall(divide, errorHandler, 10, 0)

-- Checking the result
if success then
    print("Result:", result)
else
    print(result)  -- This will print the customized error message from the errorHandler function
end

Assert

The assert function in Lua is not specifically used for handling exceptions or errors like pcall or xpcall. Instead, assert is employed to check whether a given condition is true. If the condition is false, assert will raise an error with an optional message.

While this can seem insignificant at first glance, we can create custom error messages and we can handle them with pcall, for example, if you are creating a command system and an admin player uses a command which does not exist you can throw a custom error with assert and display it to the player that executed the command

We can use the assert method this way:

  • assert(conditionToCheck, errorMessage)

Use Example:

-- Function that checks a condition using assert
function divide(a, b)
    assert(b ~= 0, "Division by zero is not allowed")
    return a / b
end

-- Example of using the divide function with assert
local result = divide(10, 2)
print("Result:", result)

-- Attempting division by zero, which will raise an error
result = divide(5, 0)
print("Result:", result)  -- This line will not be executed if there is an error in the divide function

Error method

Both assert and the error method can be used to throw custom errors, but assert is commonly used to check conditions and throw errors if those conditions are not met, while error() is used directly to throw errors with specific messages. Both can be handled by pcall as needed.

We can use the error method this way:

  • error(errorMessage)

Use Example:

-- Function that uses error and will be handled by pcall
function divide(a, b)
    if b == 0 then
        error("Division by zero is not allowed")
    end
    return a / b
end

-- Using pcall to handle errors
local success, result = pcall(function()
    return divide(10, 2)
end)

if not success then
    print("Error:", result)
else
    print("Result:", result)
end

Mini world script error handling

Mini world has some ways to handle errorsm if some of the methods of the API have any error for whatever reason, there is a way for us to handler them, the first thing that most methods return is the ErrorCode, there are two ErrorCodes:

  • ErrorCode.OK (There were no errors when executing the method, its value is 0)
  • ErrorCode.FAILED (There was an error when executing the method, its value is 1001)

the most simple form to use this is creating a variable and using the method there, this form result = yourmethod(yourargs)

Use example:

  local result = Player:setPosition(0,100,7,100) 
  if result == 1001 then --An error was found
    -- DO something
  else --There werent any errors
    -- DO something
  end
 

1)

1)
This example was found in the Datatype page
developer_center/developer_editor/script/miniapi_tutorialerrorhandling.txt · Last modified: 2024/02/06 23:13 by notsopro