comparison 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
parents 0d4146cf9fbc
children c065b7670c89
comparison
equal deleted inserted replaced
3841:b5d367798570 3842:501c7edc8c37
281 module:hook("iq/host", handle_stanza, -1); 281 module:hook("iq/host", handle_stanza, -1);
282 module:hook("message/host", handle_stanza, -1); 282 module:hook("message/host", handle_stanza, -1);
283 module:hook("presence/host", handle_stanza, -1); 283 module:hook("presence/host", handle_stanza, -1);
284 end 284 end
285 end 285 end
286
287 local http_server = require "net.http.server";
288 module:hook_object_event(http_server, "http-error", function (event)
289 local request, response = event.request, event.response;
290 if true or decide_type(request and request.headers.accept or "") == "application/json" then
291 if response then
292 response.headers.content_type = "application/json";
293 end
294 return json.encode({
295 type = "error",
296 error = event.error,
297 code = event.code,
298 });
299 end
300 end, 10);