Mercurial > prosody-hg
comparison plugins/mod_http_errors.lua @ 13959:1b95d50cfa18
mod_http_errors: Return errors as JSON if requester prefers that
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 26 Sep 2025 12:43:38 +0100 |
| parents | f72ca74de456 |
| children | 36d361722889 |
comparison
equal
deleted
inserted
replaced
| 13958:72964be32b26 | 13959:1b95d50cfa18 |
|---|---|
| 1 module:set_global(); | 1 module:set_global(); |
| 2 | 2 |
| 3 local server = require "prosody.net.http.server"; | 3 local server = require "prosody.net.http.server"; |
| 4 local codes = require "prosody.net.http.codes"; | 4 local codes = require "prosody.net.http.codes"; |
| 5 local http_errors = require "prosody.net.http.errors".registry; | |
| 6 local json = require "prosody.util.json"; | |
| 7 local new_short_id = require "prosody.util.id".short; | |
| 5 local xml_escape = require "prosody.util.stanza".xml_escape; | 8 local xml_escape = require "prosody.util.stanza".xml_escape; |
| 6 local render = require "prosody.util.interpolation".new("%b{}", xml_escape); | 9 local render = require "prosody.util.interpolation".new("%b{}", xml_escape); |
| 10 | |
| 7 | 11 |
| 8 local show_private = module:get_option_boolean("http_errors_detailed", false); | 12 local show_private = module:get_option_boolean("http_errors_detailed", false); |
| 9 local always_serve = module:get_option_boolean("http_errors_always_show", true); | 13 local always_serve = module:get_option_boolean("http_errors_always_show", true); |
| 10 local default_message = { module:get_option_string("http_errors_default_message", "That's all I know.") }; | 14 local default_message = { module:get_option_string("http_errors_default_message", "That's all I know.") }; |
| 11 local default_messages = { | 15 local default_messages = { |
| 75 event.response.headers.content_type = "text/html; charset=utf-8"; | 79 event.response.headers.content_type = "text/html; charset=utf-8"; |
| 76 end | 80 end |
| 77 return get_page(event.code, (show_private and event.private_message) or event.message or (event.error and event.error.text)); | 81 return get_page(event.code, (show_private and event.private_message) or event.message or (event.error and event.error.text)); |
| 78 end); | 82 end); |
| 79 | 83 |
| 84 -- Return error object as JSON if that's what requester prefers | |
| 85 module:hook_object_event(server, "http-error", function (event) | |
| 86 if event.request.headers.accept:match("^application/json") then | |
| 87 event.response.headers.content_type = "application/json"; | |
| 88 local error = event.request and event.error; | |
| 89 if not error then | |
| 90 error = http_errors[event.response.status_code]; | |
| 91 end | |
| 92 local extra = error.extra; | |
| 93 return json.encode({ | |
| 94 instance_id = error.instance_id or new_short_id(); | |
| 95 type = error.type; | |
| 96 condition = error.condition; | |
| 97 text = error.text or (show_private and event.private_message) or event.message; | |
| 98 namespace = extra and extra.namespace; | |
| 99 subcondition = extra and extra.condition; | |
| 100 source = error.source or "mod_http_errors"; | |
| 101 }); | |
| 102 end | |
| 103 end, 0.1); | |
| 104 | |
| 80 -- Way to use the template for other things so to give a consistent appearance | 105 -- Way to use the template for other things so to give a consistent appearance |
| 81 module:hook("http-message", function (event) | 106 module:hook("http-message", function (event) |
| 82 if event.response then | 107 if event.response then |
| 83 event.response.headers.content_type = "text/html; charset=utf-8"; | 108 event.response.headers.content_type = "text/html; charset=utf-8"; |
| 84 end | 109 end |
