changeset 3931:2e8b284ac8b3

mod_rest: Add an XML error formatter (fixes #1499)
author Kim Alvefur <zash@zash.se>
date Sat, 07 Mar 2020 17:35:09 +0100
parents d5dafd617cd6
children 8b34222216f4
files mod_rest/mod_rest.lua
diffstat 1 files changed, 15 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mod_rest/mod_rest.lua	Sat Mar 07 17:34:17 2020 +0100
+++ b/mod_rest/mod_rest.lua	Sat Mar 07 17:35:09 2020 +0100
@@ -394,13 +394,27 @@
 
 local supported_errors = {
 	"text/html",
+	"application/xmpp+xml",
 	"application/json",
 };
 
 local http_server = require "net.http.server";
 module:hook_object_event(http_server, "http-error", function (event)
 	local request, response = event.request, event.response;
-	if decide_type(request and request.headers.accept or "", supported_errors) == "application/json" then
+	local response_as = decide_type(request and request.headers.accept or "", supported_errors);
+	if response_as == "application/xmpp+xml" then
+		if response then
+			response.headers.content_type = "application/xmpp+xml";
+		end
+		local stream_error = st.stanza("error", { xmlns = "http://etherx.jabber.org/streams" });
+		if event.error then
+			stream_error:tag(event.error.condition, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }):up();
+			if event.error.text then
+				stream_error:text_tag("text", event.error.text, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' });
+			end
+		end
+		return tostring(stream_error);
+	elseif response_as == "application/json" then
 		if response then
 			response.headers.content_type = "application/json";
 		end