How do you throw Lua error up?
Asked Answered
D

3

20

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

Distributive answered 1/3, 2016 at 23:44 Comment(6)
Lua code can explicitly generate an error by calling the error function.Cartwright
@TomBlodget, make it an answer? ;)Married
@PaulKulchenko - It seems that the idea of writing comments instead of answers is quite infectious ;-)Takeover
@EgorSkriptunoff, I noticed ;)Married
@PaulKulchenko I'd rather see this question deleted than to write an answer. But, I did want to help the asker learn that intelligible documentation (which the Lua reference manual is) is a primary reference. I don't think that this question and any direct answers to it are helpful to others. If the question was broader then they would be.Cartwright
@TomBlodget: But the error still gets thrown from within aSimpleFunction() I was already aware error throws an error but the stack trace is not coming from the callerDistributive
D
24

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 
Distributive answered 3/3, 2016 at 0:1 Comment(0)
W
9

Use the error function.

error("something went wrong!")
Warhol answered 2/3, 2016 at 2:58 Comment(0)
E
2

Catching an error is as simple as using pcall

My_Error()
    --Error Somehow
end

local success,err = pcall(My_Error)

if not success then
    error(err)
end

Undoubtedly you're asking how this works. Well pcall runs a function in a protected thread (protected-call) and returns a bool, if it ran successfully, and a value (what it returned/the error).

Also don't think this means arguments to the function are impossible, simply pass them to pcall as well:

My_Error(x)
    print(x)
    --Error Somehow
end

local success,err = pcall(My_Error, "hi")

if not success then
    error(err)
end

For more error handling control, see http://www.lua.org/manual/5.3/manual.html#2.3 and http://wiki.roblox.com/index.php?title=Function_dump/Basic_functions#xpcall

Embroider answered 2/3, 2016 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.