Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you throw Lua error up?

Is it possible to throw a Lua error from a function to be handled by the script calling the function?

For example the following will throw an error at indicated comment

local function aSimpleFunction(...)
    string.format(...) -- Error is indicated to be here
end

aSimpleFunction("An example function: %i",nil)

But what I would rather do is catch the error and throw out a custom error by the function caller

local function aSimpleFunction(...)
    if pcall(function(...)
        string.format(...)
    end) == false then
       -- I want to throw a custom error to whatever is making the call to this function
    end

end

aSimpleFunction("An example function: %i",nil) -- Want the error to start unwinding here 

The intention being that in my actual use cases my functions would be more complicated and I would like to provide more meaningful error messages

like image 921
Puddler Avatar asked Mar 01 '16 23:03

Puddler


People also ask

How do you throw an error in Lua?

If you need to handle errors in Lua, you should use the pcall function (protected call) to encapsulate your code. Of course, you can call pcall with an anonymous function: if pcall(function () ...

What is a Lua programming error?

What Are Lua Errors? A Lua error is caused when the code that is being ran is improper. There are many reasons for why a Lua error might occur, but understanding what a Lua error is and how to read it is an important skill that any developer needs to have.

What is Lua Pcall?

Lua Error Handling Using pcall pcall stands for "protected call". It is used to add error handling to functions. pcall works similar as try-catch in other languages. The advantage of pcall is that the whole execution of the script is not being interrupted if errors occur in functions called with pcall .


2 Answers

The stack level of an error can be specified when throwing a new error

error("Error Message") -- Throws at the current stack
error("Error Message",2) -- Throws to the caller
error("Error Message",3) -- Throws to the caller after that

Usually, error adds some information about the error position at the beginning of the message. The level argument specifies how to get the error position. With level 1 (the default), the error position is where the error function was called. Level 2 points the error to where the function that called error was called; and so on. Passing a level 0 avoids the addition of error position information to the message.

Using the example given in the question

local function aSimpleFunction(...)
    if pcall(function(...)
        string.format(...)
    end) == false then
       error("Function cannot format text",2)
    end

end

aSimpleFunction("An example function: %i",nil) --Error appears here 
like image 51
Puddler Avatar answered Sep 26 '22 01:09

Puddler


Use the error function.

error("something went wrong!")
like image 23
Colonel Thirty Two Avatar answered Sep 25 '22 01:09

Colonel Thirty Two