Changeset

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
parents 3841:b5d367798570
children 3843:c065b7670c89
files mod_rest/mod_rest.lua
diffstat 1 files changed, 15 insertions(+), 0 deletions(-) [+]
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);