mod_rest/mod_rest.lua
changeset 3846 501c7edc8c37
parent 3836 0d4146cf9fbc
child 3847 c065b7670c89
--- 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);