util.error: Extract error originator from stanza errors
authorKim Alvefur <zash@zash.se>
Sat, 26 Sep 2020 18:13:27 +0200
changeset 11093 35d2260644d9
parent 11092 1f84d0e4d0c4
child 11094 33b6fbdcec88
util.error: Extract error originator from stanza errors
spec/util_error_spec.lua
util/error.lua
--- a/spec/util_error_spec.lua	Sat Sep 26 18:12:18 2020 +0200
+++ b/spec/util_error_spec.lua	Sat Sep 26 18:13:27 2020 +0200
@@ -48,12 +48,13 @@
 		it("works", function ()
 			local st = require "util.stanza";
 			local m = st.message({ type = "chat" });
-			local e = st.error_reply(m, "modify", "bad-request");
+			local e = st.error_reply(m, "modify", "bad-request", nil, "error.example");
 			local err = errors.from_stanza(e);
 			assert.truthy(errors.is_err(err));
 			assert.equal("modify", err.type);
 			assert.equal("bad-request", err.condition);
 			assert.equal(e, err.context.stanza);
+			assert.equal("error.example", err.context.by);
 		end);
 	end);
 
--- a/util/error.lua	Sat Sep 26 18:12:18 2020 +0200
+++ b/util/error.lua	Sat Sep 26 18:13:27 2020 +0200
@@ -93,12 +93,17 @@
 
 local function from_stanza(stanza, context)
 	local error_type, condition, text = stanza:get_error();
+	local error_tag = stanza:get_child("error");
+	context = context or {};
+	context.stanza = stanza;
+	context.by = error_tag.attr.by;
 	return setmetatable({
 		type = error_type or "cancel";
 		condition = condition or "undefined-condition";
 		text = text;
 
-		context = context or { stanza = stanza };
+		context = context;
+
 	}, error_mt);
 end