comparison mod_rest/mod_rest.lua @ 3872:513a8a7fab41

mod_rest: Hide JSON error behind a flag It's a bit overzealous and returns JSON errors even if you ask for HTML.
author Kim Alvefur <zash@zash.se>
date Mon, 03 Feb 2020 23:09:38 +0100
parents 839224be5299
children fea0c1bed1a0
comparison
equal deleted inserted replaced
3871:e5d08bb58155 3872:513a8a7fab41
313 module:hook("message/host", handle_stanza, -1); 313 module:hook("message/host", handle_stanza, -1);
314 module:hook("presence/host", handle_stanza, -1); 314 module:hook("presence/host", handle_stanza, -1);
315 end 315 end
316 end 316 end
317 317
318 local http_server = require "net.http.server"; 318 if module:get_option_boolean("rest_json_errors", false) then
319 module:hook_object_event(http_server, "http-error", function (event) 319 local http_server = require "net.http.server";
320 local request, response = event.request, event.response; 320 module:hook_object_event(http_server, "http-error", function (event)
321 if true or decide_type(request and request.headers.accept or "") == "application/json" then 321 local request, response = event.request, event.response;
322 if response then 322 if true or decide_type(request and request.headers.accept or "") == "application/json" then
323 response.headers.content_type = "application/json"; 323 if response then
324 end 324 response.headers.content_type = "application/json";
325 return json.encode({ 325 end
326 type = "error", 326 return json.encode({
327 error = event.error, 327 type = "error",
328 code = event.code, 328 error = event.error,
329 }); 329 code = event.code,
330 end 330 });
331 end, 10); 331 end
332 end, 10);
333 end