mirror of
https://github.com/bjc/prosody.git
synced 2025-04-03 21:27:38 +03:00
util.error: Expand compact registries into normal form internally
Also the exposed form on the table returned from init()
This commit is contained in:
parent
5da983d8bd
commit
9dbdb91c47
2 changed files with 66 additions and 8 deletions
|
@ -115,6 +115,38 @@ describe("util.error", function ()
|
|||
assert.equal("spec", nope.extra.namespace);
|
||||
assert.equal("sorry-dave", nope.extra.condition);
|
||||
end);
|
||||
|
||||
it("registry looks the same regardless of syntax", function()
|
||||
local normal = errors.init("test", {
|
||||
broke = {type = "cancel"; condition = "internal-server-error"; text = "It broke :("};
|
||||
nope = {
|
||||
type = "auth";
|
||||
condition = "not-authorized";
|
||||
text = "Can't let you do that Dave";
|
||||
extra = {namespace = "spec"; condition = "sorry-dave"};
|
||||
};
|
||||
});
|
||||
local compact1 = errors.init("test", {
|
||||
namespace = "spec";
|
||||
broke = {"cancel"; "internal-server-error"; "It broke :("};
|
||||
nope = {"auth"; "not-authorized"; "Can't let you do that Dave"; "sorry-dave"};
|
||||
});
|
||||
local compact2 = errors.init("test", "spec", {
|
||||
broke = {"cancel"; "internal-server-error"; "It broke :("};
|
||||
nope = {"auth"; "not-authorized"; "Can't let you do that Dave"; "sorry-dave"};
|
||||
});
|
||||
local compact3 = errors.init("test", {
|
||||
broke = {"cancel"; "internal-server-error"; "It broke :("};
|
||||
nope = {"auth"; "not-authorized"; "Can't let you do that Dave"};
|
||||
});
|
||||
assert.same(normal.registry, compact1.registry);
|
||||
assert.same(normal.registry, compact2.registry);
|
||||
|
||||
assert.same({
|
||||
broke = {type = "cancel"; condition = "internal-server-error"; text = "It broke :("};
|
||||
nope = {type = "auth"; condition = "not-authorized"; text = "Can't let you do that Dave"};
|
||||
}, compact3.registry);
|
||||
end);
|
||||
end);
|
||||
|
||||
end);
|
||||
|
|
|
@ -58,14 +58,11 @@ local function new(e, context, registry, source)
|
|||
local error_instance = setmetatable({
|
||||
instance_id = id.short();
|
||||
|
||||
type = template.type or template[1] or "cancel";
|
||||
condition = template.condition or template[2] or "undefined-condition";
|
||||
text = template.text or template[3];
|
||||
type = template.type or "cancel";
|
||||
condition = template.condition or "undefined-condition";
|
||||
text = template.text;
|
||||
code = template.code;
|
||||
extra = template.extra or (registry and registry.namespace and template[4] and {
|
||||
namespace = registry.namespace;
|
||||
condition = template[4]
|
||||
});
|
||||
extra = template.extra;
|
||||
|
||||
context = context;
|
||||
source = source;
|
||||
|
@ -74,7 +71,36 @@ local function new(e, context, registry, source)
|
|||
return error_instance;
|
||||
end
|
||||
|
||||
local function init(source, registry)
|
||||
-- compact --> normal form
|
||||
local function expand_registry(namespace, registry)
|
||||
local mapped = {}
|
||||
for err,template in pairs(registry) do
|
||||
local e = {
|
||||
type = template[1];
|
||||
condition = template[2];
|
||||
text = template[3];
|
||||
};
|
||||
if namespace and template[4] then
|
||||
e.extra = { namespace = namespace, condition = template[4] };
|
||||
end
|
||||
mapped[err] = e;
|
||||
end
|
||||
return mapped;
|
||||
end
|
||||
|
||||
local function init(source, namespace, registry)
|
||||
if type(namespace) == "table" then
|
||||
-- registry can be given as second argument if namespace is either not used
|
||||
registry, namespace = namespace, nil;
|
||||
if type(registry.namespace) == "string" then
|
||||
-- error templates are always type table, so this can't be one
|
||||
namespace, registry.namespace = registry.namespace, nil;
|
||||
end
|
||||
end
|
||||
local _, protoerr = next(registry, nil);
|
||||
if protoerr and type(next(protoerr)) == "number" then
|
||||
registry = expand_registry(namespace, registry);
|
||||
end
|
||||
return {
|
||||
source = source;
|
||||
registry = registry;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue