util/error.lua
changeset 11054 51be24b16e8a
parent 10505 e8186aba1583
child 11055 08539aa129ee
equal deleted inserted replaced
11053:f103f59ea2b5 11054:51be24b16e8a
     4 	return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text or "");
     4 	return ("error<%s:%s:%s>"):format(self.type, self.condition, self.text or "");
     5 end
     5 end
     6 
     6 
     7 local function is_err(e)
     7 local function is_err(e)
     8 	return getmetatable(e) == error_mt;
     8 	return getmetatable(e) == error_mt;
       
     9 end
       
    10 
       
    11 local auto_inject_traceback = false;
       
    12 
       
    13 local function configure(opt)
       
    14 	if opt.auto_inject_traceback ~= nil then
       
    15 		auto_inject_traceback = opt.auto_inject_traceback;
       
    16 	end
     9 end
    17 end
    10 
    18 
    11 -- Do we want any more well-known fields?
    19 -- Do we want any more well-known fields?
    12 -- Or could we just copy all fields from `e`?
    20 -- Or could we just copy all fields from `e`?
    13 -- Sometimes you want variable details in the `text`, how to handle that?
    21 -- Sometimes you want variable details in the `text`, how to handle that?
    15 -- Should the `type` be restricted to the stanza error types or free-form?
    23 -- Should the `type` be restricted to the stanza error types or free-form?
    16 -- What to set `type` to for stream errors or SASL errors? Those don't have a 'type' attr.
    24 -- What to set `type` to for stream errors or SASL errors? Those don't have a 'type' attr.
    17 
    25 
    18 local function new(e, context, registry)
    26 local function new(e, context, registry)
    19 	local template = (registry and registry[e]) or e or {};
    27 	local template = (registry and registry[e]) or e or {};
       
    28 	context = context or template.context or { _error_id = e };
       
    29 
       
    30 	if auto_inject_traceback then
       
    31 		context.traceback = debug.traceback("error stack", 2);
       
    32 	end
       
    33 
    20 	return setmetatable({
    34 	return setmetatable({
    21 		type = template.type or "cancel";
    35 		type = template.type or "cancel";
    22 		condition = template.condition or "undefined-condition";
    36 		condition = template.condition or "undefined-condition";
    23 		text = template.text;
    37 		text = template.text;
    24 		code = template.code;
    38 		code = template.code;
    55 return {
    69 return {
    56 	new = new;
    70 	new = new;
    57 	coerce = coerce;
    71 	coerce = coerce;
    58 	is_err = is_err;
    72 	is_err = is_err;
    59 	from_stanza = from_stanza;
    73 	from_stanza = from_stanza;
       
    74 	configure = configure;
    60 }
    75 }