Sunday, January 12, 2020

Know Your Lua Errors

One problem when trying to help people with their Lua problem is that they don't know how to read Lua errors. You can be 100% sure that saying "it doesn't work" is absolutely not helpful at all. So I'll tell you the ways to read Lua errors properly and what it means.

In this post, I used "<>" symbol to denote something arbitrary. Start by reading the syntax of Lua error. Lua error is usually have syntax like this:

<filename>:<lineNumber>: <message>

<filename> is the script file. <lineNumber> is line number where it occured. That's very easy to remember, right? Now for the error message. The error message is usually very descriptive but sometimes it doesn't really tell you what exactly is wrong, so here are some lists of Lua errors that I'm aware of. If your Lua error is unlisted, that can mean I didn't add it yet or it's thrown by external program.
  1. attempt to call global '<name>' (a <non-function> value)
    This caused when you're trying to call a global variable called '<name>' but '<name>' is not a function type. Example
    print() -- works
    table() -- attempt to call global 'table' (a table value)
    _VERSION() -- attempt to call global '_VERSION' (a string value)
    anilvalue() -- attempt to call global 'anilvalue' (a nil value)
    
  2. attempt to call field '<field>' (a <non-function> value)
    Similar to above, but this occurs when you try to call something within a table.
    -- Note that 'math' is a global variable which is table
    math.abs(123) -- works
    math.pi() -- attempt to call field 'pi' (a number value)
    
  3. bad argument #<n> to '<name>' (<type1> expected, got <type2>)
    This is caused when a function '<name>' expects value with type <type1> for n-th argument, but user passed something with type <type2> instead.
    -- io.open expects string for the 1st argument
    local file = io.open(io.open) -- bad argument #1 to 'open' (string expected, got function)
    -- tonumber 2nd argument expects number if present
    tonumber("0xFF") -- works
    tonumber("0xFF", table) -- bad argument #2 to 'tonumber' (number expected, got table)
    
  4. table index is nil
    To be honest, this is most undescriptive Lua error message. What it means that you try to assign a value to a table at index "nil" (I mean literal nil).
    table[nil] = io -- table index is nil
    
  5. bad argument #<n> to '<name>' (invalid option '<something>')
    This means you passed invalid option. Notable function that throw this is 'collectgarbage' and 'file:seek'.
    print(collectgarbage("count")) -- works
    collectgarbage("asd") -- bad argument #1 to 'collectgarbage' (invalid option 'asd')
    
So I think that covers most common Lua errors that you mostly encounter. In case you need help, please provide the Lua error message, plus the traceback if available. The traceback is also easy to read, the syntax is similar to above, and it definely helps.

... unless you got a very rare "PANIC" error which is unhelpful. No, really.