Comparison

util/error.lua @ 11101:2288d206b14b

util.error: Expand compact registries into normal form internally Also the exposed form on the table returned from init()
author Kim Alvefur <zash@zash.se>
date Mon, 28 Sep 2020 22:13:04 +0200
parent 11100:3aa06cdd2dc8
child 11102:5a0ff475ecfd
comparison
equal deleted inserted replaced
11100:3aa06cdd2dc8 11101:2288d206b14b
56 end 56 end
57 57
58 local error_instance = setmetatable({ 58 local error_instance = setmetatable({
59 instance_id = id.short(); 59 instance_id = id.short();
60 60
61 type = template.type or template[1] or "cancel"; 61 type = template.type or "cancel";
62 condition = template.condition or template[2] or "undefined-condition"; 62 condition = template.condition or "undefined-condition";
63 text = template.text or template[3]; 63 text = template.text;
64 code = template.code; 64 code = template.code;
65 extra = template.extra or (registry and registry.namespace and template[4] and { 65 extra = template.extra;
66 namespace = registry.namespace;
67 condition = template[4]
68 });
69 66
70 context = context; 67 context = context;
71 source = source; 68 source = source;
72 }, error_mt); 69 }, error_mt);
73 70
74 return error_instance; 71 return error_instance;
75 end 72 end
76 73
77 local function init(source, registry) 74 -- compact --> normal form
75 local function expand_registry(namespace, registry)
76 local mapped = {}
77 for err,template in pairs(registry) do
78 local e = {
79 type = template[1];
80 condition = template[2];
81 text = template[3];
82 };
83 if namespace and template[4] then
84 e.extra = { namespace = namespace, condition = template[4] };
85 end
86 mapped[err] = e;
87 end
88 return mapped;
89 end
90
91 local function init(source, namespace, registry)
92 if type(namespace) == "table" then
93 -- registry can be given as second argument if namespace is either not used
94 registry, namespace = namespace, nil;
95 if type(registry.namespace) == "string" then
96 -- error templates are always type table, so this can't be one
97 namespace, registry.namespace = registry.namespace, nil;
98 end
99 end
100 local _, protoerr = next(registry, nil);
101 if protoerr and type(next(protoerr)) == "number" then
102 registry = expand_registry(namespace, registry);
103 end
78 return { 104 return {
79 source = source; 105 source = source;
80 registry = registry; 106 registry = registry;
81 new = function (e, context) 107 new = function (e, context)
82 return new(e, context, registry, source); 108 return new(e, context, registry, source);