util/adhoc.lua
author Kim Alvefur <zash@zash.se>
Wed, 19 Apr 2023 11:32:53 +0200
branch0.12
changeset 13082 6da83deb8d7f
parent 11356 e10567199f02
permissions -rw-r--r--
util.error: Fix error on conversion of invalid error stanza, fix #1805 Error stanzas should have an <error> element, but if you pass a stanza without one to util.error.from_stanza() it triggers an attempt to index a nil value, which this patch avoids. In the conditional, it should be safe to assume error_tag is non-nil since condition can't have those values then.

-- luacheck: ignore 212/self

local function new_simple_form(form, result_handler)
	return function(self, data, state)
		if state or data.form then
			if data.action == "cancel" then
				return { status = "canceled" };
			end
			local fields, err = form:data(data.form);
			return result_handler(fields, err, data);
		else
			return { status = "executing", actions = {"next", "complete", default = "complete"}, form = form }, "executing";
		end
	end
end

local function new_initial_data_form(form, initial_data, result_handler)
	return function(self, data, state)
		if state or data.form then
			if data.action == "cancel" then
				return { status = "canceled" };
			end
			local fields, err = form:data(data.form);
			return result_handler(fields, err, data);
		else
			local values, err = initial_data(data);
			if type(err) == "table" then
				return {status = "error"; error = err}
			elseif type(err) == "string" then
				return {status = "error"; error = {type = "cancel"; condition = "internal-server-error", err}}
			end
			return { status = "executing", actions = {"next", "complete", default = "complete"},
				 form = { layout = form, values = values } }, "executing";
		end
	end
end

return { new_simple_form = new_simple_form,
	 new_initial_data_form = new_initial_data_form };