From 5da983d8bdc574f9358a16eb08d700dde3ca7026 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 28 Sep 2020 18:39:51 +0200 Subject: util.error: Add a "compact mode" for registries Inspired by the older registry in pubsub.lib.lua --- spec/util_error_spec.lua | 21 +++++++++++++++++++++ util/error.lua | 11 +++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/spec/util_error_spec.lua b/spec/util_error_spec.lua index 6314d9ff..399b5998 100644 --- a/spec/util_error_spec.lua +++ b/spec/util_error_spec.lua @@ -94,6 +94,27 @@ describe("util.error", function () assert.equal("not-authorized", nope.condition); assert.equal("Can't let you do that Dave", nope.text); end); + + it("compact mode works", function() + local reg = 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 broke = reg.new("broke"); + assert.equal("cancel", broke.type); + assert.equal("internal-server-error", broke.condition); + assert.equal("It broke :(", broke.text); + assert.is_nil(broke.extra); + + local nope = reg.new("nope"); + assert.equal("auth", nope.type); + assert.equal("not-authorized", nope.condition); + assert.equal("Can't let you do that Dave", nope.text); + assert.equal("spec", nope.extra.namespace); + assert.equal("sorry-dave", nope.extra.condition); + end); end); end); diff --git a/util/error.lua b/util/error.lua index 47d4e7b6..2e4118ec 100644 --- a/util/error.lua +++ b/util/error.lua @@ -58,11 +58,14 @@ local function new(e, context, registry, source) local error_instance = setmetatable({ instance_id = id.short(); - type = template.type or "cancel"; - condition = template.condition or "undefined-condition"; - text = template.text; + type = template.type or template[1] or "cancel"; + condition = template.condition or template[2] or "undefined-condition"; + text = template.text or template[3]; code = template.code; - extra = template.extra; + extra = template.extra or (registry and registry.namespace and template[4] and { + namespace = registry.namespace; + condition = template[4] + }); context = context; source = source; -- cgit v1.2.3