util.error: Collect Application-Specific Conditions from stanza errors
authorKim Alvefur <zash@zash.se>
Sat, 26 Sep 2020 18:15:49 +0200
changeset 11096 bd13aa89262d
parent 11095 4b4b5188492f
child 11097 ec14d4fce855
util.error: Collect Application-Specific Conditions from stanza errors
spec/util_error_spec.lua
util/error.lua
--- a/spec/util_error_spec.lua	Sat Sep 26 18:15:27 2020 +0200
+++ b/spec/util_error_spec.lua	Sat Sep 26 18:15:49 2020 +0200
@@ -48,13 +48,14 @@
 		it("works", function ()
 			local st = require "util.stanza";
 			local m = st.message({ type = "chat" });
-			local e = st.error_reply(m, "modify", "bad-request", nil, "error.example");
+			local e = st.error_reply(m, "modify", "bad-request", nil, "error.example"):tag("extra", { xmlns = "xmpp:example.test" });
 			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);
+			assert.not_nil(err.extra.tag);
 		end);
 	end);
 
--- a/util/error.lua	Sat Sep 26 18:15:27 2020 +0200
+++ b/util/error.lua	Sat Sep 26 18:15:49 2020 +0200
@@ -92,7 +92,7 @@
 end
 
 local function from_stanza(stanza, context)
-	local error_type, condition, text = stanza:get_error();
+	local error_type, condition, text, extra_tag = stanza:get_error();
 	local error_tag = stanza:get_child("error");
 	context = context or {};
 	context.stanza = stanza;
@@ -102,8 +102,9 @@
 		type = error_type or "cancel";
 		condition = condition or "undefined-condition";
 		text = text;
-		extra = condition == "gone" and {
+		extra = (extra_tag or condition == "gone") and {
 			uri = error_tag:get_child_text("gone", "urn:ietf:params:xml:ns:xmpp-stanzas");
+			tag = extra_tag;
 		} or nil;
 
 		context = context;