Comparison

util/error.lua @ 11222:4b39691a274e

util.error: rename is_err() -> is_error() More descriptive and consistent with e.g. is_promise().
author Matthew Wild <mwild1@gmail.com>
date Wed, 09 Dec 2020 13:59:51 +0000
parent 11221:b0a563716334
child 11223:b8589256b404
comparison
equal deleted inserted replaced
11221:b0a563716334 11222:4b39691a274e
12 return ("error<%s:%s:%s:%s>"):format(self.type, self.condition, self.text or "", self.context.traceback); 12 return ("error<%s:%s:%s:%s>"):format(self.type, self.condition, self.text or "", self.context.traceback);
13 end 13 end
14 return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text or ""); 14 return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text or "");
15 end 15 end
16 16
17 local function is_err(e) 17 local function is_error(e)
18 return getmetatable(e) == error_mt; 18 return getmetatable(e) == error_mt;
19 end 19 end
20 20
21 local function configure(opt) 21 local function configure(opt)
22 if opt.display_tracebacks ~= nil then 22 if opt.display_tracebacks ~= nil then
33 -- Translations? 33 -- Translations?
34 -- Should the `type` be restricted to the stanza error types or free-form? 34 -- Should the `type` be restricted to the stanza error types or free-form?
35 -- What to set `type` to for stream errors or SASL errors? Those don't have a 'type' attr. 35 -- What to set `type` to for stream errors or SASL errors? Those don't have a 'type' attr.
36 36
37 local function new(e, context, registry, source) 37 local function new(e, context, registry, source)
38 if is_err(e) then return e; end 38 if is_error(e) then return e; end
39 local template = registry and registry[e]; 39 local template = registry and registry[e];
40 if not template then 40 if not template then
41 if type(e) == "table" then 41 if type(e) == "table" then
42 template = { 42 template = {
43 code = e.code; 43 code = e.code;
98 if protoerr and type(next(protoerr)) == "number" then 98 if protoerr and type(next(protoerr)) == "number" then
99 registry = expand_registry(namespace, registry); 99 registry = expand_registry(namespace, registry);
100 end 100 end
101 101
102 local function wrap(e, context) 102 local function wrap(e, context)
103 if is_err(e) then 103 if is_error(e) then
104 return e; 104 return e;
105 end 105 end
106 local err = new(registry[e] or { 106 local err = new(registry[e] or {
107 type = "cancel", condition = "undefined-condition" 107 type = "cancel", condition = "undefined-condition"
108 }, context, registry, source); 108 }, context, registry, source);
125 wrap = wrap; 125 wrap = wrap;
126 }; 126 };
127 end 127 end
128 128
129 local function coerce(ok, err, ...) 129 local function coerce(ok, err, ...)
130 if ok or is_err(err) then 130 if ok or is_error(err) then
131 return ok, err, ...; 131 return ok, err, ...;
132 end 132 end
133 133
134 local new_err = new({ 134 local new_err = new({
135 type = "cancel", condition = "undefined-condition" 135 type = "cancel", condition = "undefined-condition"
163 163
164 return { 164 return {
165 new = new; 165 new = new;
166 init = init; 166 init = init;
167 coerce = coerce; 167 coerce = coerce;
168 is_err = is_err; 168 is_error = is_error;
169 is_err = is_error; -- COMPAT w/ older 0.12 trunk
169 from_stanza = from_stanza; 170 from_stanza = from_stanza;
170 configure = configure; 171 configure = configure;
171 } 172 }