# HG changeset patch # User Kim Alvefur # Date 1579120683 -3600 # Node ID 501c7edc8c375fb2e71a0d34a52bc3e3355c9a48 # Parent b5d36779857080aee874d792cb26e2e29c245401 mod_rest: Encode errors as JSON This applies to *all* errors in the entire HTTP server, which might be a bit awkward. diff -r b5d367798570 -r 501c7edc8c37 mod_rest/mod_rest.lua --- a/mod_rest/mod_rest.lua Thu Jan 09 21:21:09 2020 +0100 +++ b/mod_rest/mod_rest.lua Wed Jan 15 21:38:03 2020 +0100 @@ -283,3 +283,18 @@ module:hook("presence/host", handle_stanza, -1); end end + +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 true or decide_type(request and request.headers.accept or "") == "application/json" then + if response then + response.headers.content_type = "application/json"; + end + return json.encode({ + type = "error", + error = event.error, + code = event.code, + }); + end +end, 10);