Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Module 6: Errors, modules, and tests

Objectives: LO-6.1, LO-6.2, LO-6.3
Prerequisites: Module 5
Time: about 9 hours: 2.5 for this reading, 2 for Lab 6, 1.5 for the practice, and 3 for PA3, which follows this module

Programs so far have lived in one file or one notebook, and stopped at the first error. This module turns them into code other code can rely on. It covers how a Lua function reports that it failed, and how its caller catches the failure; how to split a program into modules, files that require loads; and how to test a module with busted, the most widely used Lua test framework. PA3 asks for all three.

Two ways to report a failure

A Lua function can report a failure in two ways, and the standard library uses both:

io.open shows the second way. Opening a file that doesn’t exist isn’t a bug; the caller asked about the outside world, and the answer was no:

local file, message = io.open("missing.txt")
print(file, message)      --> nil  missing.txt: No such file or directory

Passing it the wrong type of argument is a bug in the calling code, so it raises an error instead: io.open(42, {}) stops with bad argument #2 to 'open' (string expected, got table). The rule the library follows, and a good rule for your own code:

The failure is…ExampleReport it by…
A mistake in the calling codeA wrong argument type, a missing argument, an empty list where one is requiredRaising an error
An expected outcome the caller should handleA missing file, text that isn’t a number, a key that isn’t thereReturning nil and a message

A caller can’t forget to handle an error, because it stops the program; it can forget to check a nil. So raise an error when continuing would hide a bug, and return nil when the caller has a sensible next step, such as asking for another file name.

error and its level

error(message, level) raises an error. When message is a string, Lua puts a position in front of it, file:line:, and level says whose position:

local function check_age(age)
  if type(age) ~= "number" then
    error("age must be a number", 2)
  end
  return age
end

local function register()
  local age = check_age("ten")     -- line 9: the message names this line
  return age
end

print(pcall(register))    --> false  example.lua:9: age must be a number

register calls check_age in an ordinary statement here. If it wrote return check_age("ten"), a proper tail call from module 2, register would have no stack frame left, and level 2 would find no line to name.

An error doesn’t have to be a string. error({ code = 404, reason = "missing" }) raises a table, which gets no position, and lets the code that catches it read its fields. Use a table when the catcher must tell kinds of failure apart; use a string when a person will read it.

assert

assert(value, message) raises an error with message when value is nil or false, and otherwise returns all its arguments. That makes it a one-line way to turn a nil, message result into an error, when your program can’t go on without the value:

local file = assert(io.open("config.txt"))
--> config.txt: No such file or directory, if the file is missing

assert passes io.open’s message along as its own, and adds the position of the line that called it. Without a message, it raises assertion failed!.

Catching errors

pcall(f, ...) calls f with the given arguments in protected mode: if f raises an error, pcall catches it and returns false and the error value; otherwise it returns true and all of f’s results:

print(pcall(function(a, b) return a + b, a * b end, 2, 3))    --> true  5  6
print(pcall(error, "boom", 0))                                --> false  boom
local ok, err = pcall(error, { code = 404 })
print(ok, err.code)                                           --> false  404

By the time pcall returns, the functions that raised the error have already returned, so the stack that led to the error is gone. xpcall(f, handler, ...) calls handler with the error value first, while that stack still exists. debug.traceback is the usual handler: it returns the message with a list of the calls that led to it.

local function inner() local settings = nil; return settings.width end
local function outer() return inner() + 1 end
print(xpcall(outer, debug.traceback))
--> false  example.lua:1: attempt to index a nil value (local 'settings')
--> stack traceback:
-->         example.lua:1: in upvalue 'inner'
-->         …

Three rules keep error handling honest:

Modules

A module is a Lua file that returns a value, almost always a table of functions. Lua’s own libraries are modules: string and table are tables of functions. A module keeps its helpers private by declaring them local, and creates no globals:

-- geometry.lua
local geometry = {}

local function square(x)          -- private: not in the returned table
  return x * x
end

function geometry.distance(x1, y1, x2, y2)
  return math.sqrt(square(x2 - x1) + square(y2 - y1))
end

return geometry

Another file loads it with require, which returns the module’s value. By convention the local that holds it has the module’s name:

local geometry = require("geometry")
print(geometry.distance(0, 0, 3, 4))       --> 5.0

How require finds and keeps modules

require(name) checks the cache before it looks for a loader.

  1. If package.loaded[name] is truthy, require returns that value alone. For geometry, every caller receives the same table. A cached false or nil triggers another search.

  2. On a cache miss, Lua tries its searchers in order. The Lua-file searcher reads package.path, a string of templates separated by semicolons, such as ./?.lua;./?/init.lua. It replaces dots in the module name with directory separators and substitutes the name for each ?. After running the loader, require returns the module value and the loader data. For a Lua file, that second result is the file’s path.

Lua caches any non-nil value the loader returns. If the loader returns nil and leaves package.loaded[name] unset, Lua records true.

So require("geo.point") with the template ./?.lua loads ./geo/point.lua. When no template matches, the error lists every file it tried:

module 'nope' not found:
        no field package.preload['nope']
        no file './nope.lua'
        no file '/home/ada/.venv/lua/share/lua/5.4/nope.lua'
        …

That list is the first thing to read when a module won’t load: it shows exactly where Lua looked. package.searchpath(name, package.path) runs the same search without loading anything. Two consequences catch people out:

Every place require looks

Searching package.path is one of four ways require finds a module. It asks each function in the list package.searchers in turn, and the first one that finds the module wins. The error above has a line from each searcher it asked:

  1. package.preload, a table of loader functions by module name. A program that carries a module inside itself, instead of in a file, registers it here.

  2. Lua files, along package.path, as the previous section describes.

  3. C libraries, along package.cpath, which is how Module 13 loads a module compiled from C.

  4. One C library for several modules: for require("a.b"), the library for a, if it has a function named luaopen_a_b.

A loader is a function that runs the module and returns its value. It receives the module’s name, and a second value from the searcher that found it, which require also returns as its second result: the file’s path for a file, and the string ":preload:" for package.preload.

package.preload.greet = function(name, extra)
  return { hello = function() return "hi from " .. name .. " (" .. extra .. ")" end }
end
print(require("greet").hello())   --> hi from greet (:preload:)

A searcher of your own is a function that takes the module’s name and returns a loader and the second value. If it can’t find the module, it returns a string saying why, and require adds that string to its error message. This one serves modules kept in a table of source text, as a program bundled into one file might:

local sources = { shapes = "local M = {} function M.area(w, h) return w * h end return M" }

table.insert(package.searchers, 2, function(name)   -- after preload, before the files
  local code = sources[name]
  if not code then return "no embedded module '" .. name .. "'" end
  return assert(load(code, "=" .. name, "t")), "embedded:" .. name
end)

local shapes, where = require("shapes")
print(shapes.area(3, 4), where)   --> 12  embedded:shapes

After this, require("nope") reports no embedded module 'nope' between the line for package.preload and the lines for files. Tests use the first searcher in a simpler way: setting package.loaded.clock, or package.preload.clock, before the code under test requires clock gives that code a stand-in instead of the real module. Lua 5.1 and LuaJIT call the list package.loaders.

Testing with busted

A unit test calls one function with chosen inputs and checks its results. busted, installed with LuaRocks in setting up, runs tests written in spec files, whose names end in _spec.lua:

-- spec/geometry_spec.lua
local geometry = require("geometry")

describe("geometry.distance", function()
  it("measures a 3-4-5 triangle", function()
    assert.are.equal(5, geometry.distance(0, 0, 3, 4))
  end)

  it("is zero from a point to itself", function()
    assert.are.equal(0, geometry.distance(2, 7, 2, 7))
  end)
end)

describe groups tests, and it defines one test, which passes if its function finishes and fails at the first assertion that doesn’t hold. Run busted from the project’s folder, which runs every spec in spec/, or name a spec file. --lpath=PATH adds a template to package.path:

busted --lpath="./?.lua" spec/geometry_spec.lua
●●
2 successes / 0 failures / 0 errors / 0 pending : 0.002 seconds

In a terminal, each green ● is a passing test, and a failing one is a red ◼; when busted’s output goes to a file or a notebook instead, it prints + and -. For each failure, busted prints which assertion failed, the value it got, and the value it expected. busted exits with a nonzero status when any test fails, so scripts and continuous integration can rely on it.

Assertions

AssertionPasses when
assert.are.equal(expected, actual)expected == actual; for tables, that means the same table
assert.are.same(expected, actual)The two values have the same contents, compared deeply
assert.is_true(v), assert.is_nil(v)v is exactly true, or nil
assert.has_error(fn, message)Calling fn raises an error whose message, without its position, is message
assert.has_no.errors(fn)Calling fn raises no error
assert.error_matches(fn, pattern)Calling fn raises an error whose message matches the Lua pattern

has_error takes a function, not a call: assert.has_error(function() stats.mean({}) end, "…"). Writing assert.has_error(stats.mean({})) calls stats.mean before the assertion runs, so the error stops the test instead of being checked.

Testing the failure paths

A test of a function that only checks its normal results misses half of what it promises. For each function, test:

describe("stats.parse", function()
  it("returns nil and a message for text that isn't a number", function()
    local value, message = stats.parse("ten")
    assert.is_nil(value)
    assert.are.equal("not a number: ten", message)
  end)
end)

describe("stats.mean", function()
  it("raises an error for an empty list", function()
    assert.has_error(function() stats.mean({}) end, "mean: expected a non-empty list")
  end)
end)

A good way to judge a test suite is to break the code on purpose and see whether a test fails. A suite that still passes when mean returns 0 for an empty list instead of raising an error doesn’t test that error. Lab 6 checks your tests this way.

Common misconceptions

BeliefWhat Lua doesExample that breaks it
Every failure should raise an errorThe library returns nil, message for expected failuresio.open("missing.txt") returns nil and a message
error(msg, 2) always names the caller’s lineNot after a tail call, which leaves no caller framereturn check_age("ten") gives a message with no position
pcall(f(x)) protects the callIt calls f(x) first, then passes the result to pcallWrite pcall(f, x)
An error must be a stringAny value can be raisederror({ code = 404 })
require reruns the file each timeIt returns the cached value from package.loadedEdit a module in a notebook, and require still gives the old one
require looks next to the calling fileIt searches package.path, from the current directoryA program started from another folder can’t find its module
assert.has_error(f()) checks f’s errorIt calls f too earlyPass function() f() end
Tests of the normal case are enoughFailure paths need tests of their ownA broken error check passes every normal-case test

Summary

Self-check

  1. A function load_config(name) reads a settings file. Should a missing file raise an error or return nil, message? Should a name that isn’t a string?

    Answer

    A missing file should return nil, message: it is an expected outcome, and the caller may want to fall back to the defaults. A name that isn’t a string is a bug in the calling code, so it should raise an error, at level 2.

  2. What does print(pcall(error, "boom")) print, and what does print(pcall(error("boom"))) do?

    Answer

    The first prints false boom: pcall calls error directly, from C, so there is no Lua line to name. The second calls error("boom") before pcall runs, so the error isn’t caught, and the program stops.

  3. A notebook cell runs local m = require("stats") after you fixed a bug in stats.lua, but the bug is still there. Why, and what is the fix?

    Answer

    require found stats in package.loaded and returned the module it loaded before the fix. Run package.loaded.stats = nil first, then require loads the file again.

  4. With package.path set to "./lib/?.lua;./?.lua", which files does require("text.wrap") try, and in what order?

    Answer

    ./lib/text/wrap.lua, then ./text/wrap.lua. The dot becomes a directory separator, and the templates are tried from left to right. (package.preload is checked before either.)

  5. A test suite for mean checks mean({ 2, 4 }) == 3 and nothing else. Give a broken version of mean that still passes, and the test that would catch it.

    Answer

    A version that returns 0 for an empty list, instead of raising an error, passes. So does one that returns list[1] + 1. Add assert.has_error(function() mean({}) end, "…"), and a second case such as mean({ 1, 2, 6 }) == 3.

Further reading