util.error

The util.error library implements a common object for passing around errors.

TL;DR

local errors = require "util.error";

local function fail()
    return nil, errors.new({ text = "It didn't work" });
end

Creating a new error

new(error, context, registry) --> error

The new() function takes up to 3 arguments and returns a new error object. Easiest is to pass a table with type, condition and text fields (all optional).

Fields

These mostly correspond to XMPP stanza errors.

type
Which basic kind of error is. Defaults to cancel. Other common values are auth, modify or wait.
condition
The specific error condition. Defaults to undefined-condition.
text
Human-readable textual description of what happened. Often shown to users, so keep that in mind.
code
Numeric error code. Defaults to 500. Used e.g. in HTTP.
context
Any extra data included with the error.

Using error registries

An example:

local thing_errors = {
    broke = {
        text = "It broke";
    };
    fail = {
        text = "It failed";
    };
};

function try()
    return errors.new("broke", nil, thing_errors);
end