util.stanza: Accept util.error object to error_reply
authorKim Alvefur <zash@zash.se>
Sat, 14 Dec 2019 22:47:41 +0100
changeset 10507 e25a1a9a6e7e
parent 10506 f1c0aa521dd5
child 10508 1f80e5ad3cea
util.stanza: Accept util.error object to error_reply If we're moving towards util.error as the standard error container then this makes sense. This may allow for future extensibility without needing a lot of optional arguments.
spec/util_stanza_spec.lua
util/stanza.lua
--- a/spec/util_stanza_spec.lua	Sat Dec 14 22:43:12 2019 +0100
+++ b/spec/util_stanza_spec.lua	Sat Dec 14 22:47:41 2019 +0100
@@ -1,5 +1,6 @@
 
 local st = require "util.stanza";
+local errors = require "util.error";
 
 describe("util.stanza", function()
 	describe("#preserialize()", function()
@@ -231,6 +232,22 @@
 			end, "got stanza of type error");
 		end);
 
+		it("should accept util.error objects", function ()
+			local s = st.message({ to = "touser", from = "fromuser", id = "123", type = "chat" }, "Hello");
+			local e = errors.new({ type = "modify", condition = "not-acceptable", text = "Bork bork bork" });
+			local r = st.error_reply(s, e);
+
+			assert.are.equal(r.name, s.name);
+			assert.are.equal(r.id, s.id);
+			assert.are.equal(r.attr.to, s.attr.from);
+			assert.are.equal(r.attr.from, s.attr.to);
+			assert.are.equal(r.attr.type, "error");
+			assert.are.equal(r.tags[1].name, "error");
+			assert.are.equal(r.tags[1].attr.type, e.type);
+			assert.are.equal(r.tags[1].tags[1].name, e.condition);
+			assert.are.equal(r.tags[1].tags[2]:get_text(), e.text);
+		end);
+
 	end);
 
 	describe("should reject #invalid", function ()
--- a/util/stanza.lua	Sat Dec 14 22:43:12 2019 +0100
+++ b/util/stanza.lua	Sat Dec 14 22:47:41 2019 +0100
@@ -458,6 +458,9 @@
 	if t.attr.from == error_by then
 		error_by = nil;
 	end
+	if type(error_type) == "table" then -- an util.error or similar object
+		error_type, condition, error_message = error_type.type, error_type.condition, error_type.text;
+	end
 	t:tag("error", {type = error_type, by = error_by}) --COMPAT: Some day xmlns:stanzas goes here
 	:tag(condition, xmpp_stanzas_attr):up();
 	if error_message then t:text_tag("text", error_message, xmpp_stanzas_attr); end