Diff

mod_rest/mod_rest.lua @ 3842:501c7edc8c37

mod_rest: Encode errors as JSON This applies to *all* errors in the entire HTTP server, which might be a bit awkward.
author Kim Alvefur <zash@zash.se>
date Wed, 15 Jan 2020 21:38:03 +0100
parent 3832:0d4146cf9fbc
child 3843:c065b7670c89
line wrap: on
line diff
--- 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);