Prepare required data folder for integration tests
[prosody.git] / util / error.lua
blob23551fe29876e2479036e0862ed04f9ba32799c4
1 local error_mt = { __name = "error" };
3 function error_mt:__tostring()
4 return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text or "");
5 end
7 local function is_err(e)
8 return getmetatable(e) == error_mt;
9 end
11 local function new(e, context, registry)
12 local template = (registry and registry[e]) or e or {};
13 return setmetatable({
14 type = template.type or "cancel";
15 condition = template.condition or "undefined-condition";
16 text = template.text;
18 context = context or template.context or { _error_id = e };
19 }, error_mt);
20 end
22 local function coerce(ok, err, ...)
23 if ok or is_err(err) then
24 return ok, err, ...;
25 end
27 local new_err = setmetatable({
28 native = err;
30 type = "cancel";
31 condition = "undefined-condition";
32 }, error_mt);
33 return ok, new_err, ...;
34 end
36 local function from_stanza(stanza, context)
37 local error_type, condition, text = stanza:get_error();
38 return setmetatable({
39 type = error_type or "cancel";
40 condition = condition or "undefined-condition";
41 text = text;
43 context = context or { stanza = stanza };
44 }, error_mt);
45 end
47 return {
48 new = new;
49 coerce = coerce;
50 is_err = is_err;
51 from_stanza = from_stanza;