Changeset

11100:3aa06cdd2dc8

util.error: Add a "compact mode" for registries Inspired by the older registry in pubsub.lib.lua
author Kim Alvefur <zash@zash.se>
date Mon, 28 Sep 2020 18:39:51 +0200
parents 11099:6fbfe2af4b4f
children 11101:2288d206b14b
files spec/util_error_spec.lua util/error.lua
diffstat 2 files changed, 28 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/spec/util_error_spec.lua	Mon Sep 28 19:32:54 2020 +0200
+++ b/spec/util_error_spec.lua	Mon Sep 28 18:39:51 2020 +0200
@@ -94,6 +94,27 @@
 			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);
--- a/util/error.lua	Mon Sep 28 19:32:54 2020 +0200
+++ b/util/error.lua	Mon Sep 28 18:39:51 2020 +0200
@@ -58,11 +58,14 @@
 	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;