# HG changeset patch # User Kim Alvefur # Date 1583598909 -3600 # Node ID 2e8b284ac8b3b4cec36836c8d69716153f09fc87 # Parent d5dafd617cd60787e7976f1d3975b5f586b4521c mod_rest: Add an XML error formatter (fixes #1499) diff -r d5dafd617cd6 -r 2e8b284ac8b3 mod_rest/mod_rest.lua --- 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