Mercurial > prosody-modules
comparison mod_rest/mod_rest.lua @ 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 | 93147b89ea67 |
comparison
equal
deleted
inserted
replaced
| 3930:d5dafd617cd6 | 3931:2e8b284ac8b3 |
|---|---|
| 392 end | 392 end |
| 393 end | 393 end |
| 394 | 394 |
| 395 local supported_errors = { | 395 local supported_errors = { |
| 396 "text/html", | 396 "text/html", |
| 397 "application/xmpp+xml", | |
| 397 "application/json", | 398 "application/json", |
| 398 }; | 399 }; |
| 399 | 400 |
| 400 local http_server = require "net.http.server"; | 401 local http_server = require "net.http.server"; |
| 401 module:hook_object_event(http_server, "http-error", function (event) | 402 module:hook_object_event(http_server, "http-error", function (event) |
| 402 local request, response = event.request, event.response; | 403 local request, response = event.request, event.response; |
| 403 if decide_type(request and request.headers.accept or "", supported_errors) == "application/json" then | 404 local response_as = decide_type(request and request.headers.accept or "", supported_errors); |
| 405 if response_as == "application/xmpp+xml" then | |
| 406 if response then | |
| 407 response.headers.content_type = "application/xmpp+xml"; | |
| 408 end | |
| 409 local stream_error = st.stanza("error", { xmlns = "http://etherx.jabber.org/streams" }); | |
| 410 if event.error then | |
| 411 stream_error:tag(event.error.condition, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }):up(); | |
| 412 if event.error.text then | |
| 413 stream_error:text_tag("text", event.error.text, {xmlns = 'urn:ietf:params:xml:ns:xmpp-streams' }); | |
| 414 end | |
| 415 end | |
| 416 return tostring(stream_error); | |
| 417 elseif response_as == "application/json" then | |
| 404 if response then | 418 if response then |
| 405 response.headers.content_type = "application/json"; | 419 response.headers.content_type = "application/json"; |
| 406 end | 420 end |
| 407 return json.encode({ | 421 return json.encode({ |
| 408 type = "error", | 422 type = "error", |
