mirror of
https://github.com/bjc/prosody.git
synced 2025-04-04 13:47:41 +03:00
Many things: switch from hacky multi-arg xpcall implementations to a standard util.xpcall
This commit is contained in:
parent
1454ac8208
commit
f5f6460b2e
7 changed files with 23 additions and 31 deletions
|
@ -18,18 +18,13 @@ local api = require "core.moduleapi"; -- Module API container
|
||||||
local prosody = prosody;
|
local prosody = prosody;
|
||||||
local hosts = prosody.hosts;
|
local hosts = prosody.hosts;
|
||||||
|
|
||||||
local xpcall = xpcall;
|
local xpcall = require "util.xpcall".xpcall;
|
||||||
local setmetatable, rawget = setmetatable, rawget;
|
local setmetatable, rawget = setmetatable, rawget;
|
||||||
local ipairs, pairs, type, tostring, t_insert = ipairs, pairs, type, tostring, table.insert;
|
local ipairs, pairs, type, tostring, t_insert = ipairs, pairs, type, tostring, table.insert;
|
||||||
|
|
||||||
local debug_traceback = debug.traceback;
|
local debug_traceback = debug.traceback;
|
||||||
local select = select;
|
local select = select;
|
||||||
local unpack = table.unpack or unpack; --luacheck: ignore 113
|
local unpack = table.unpack or unpack; --luacheck: ignore 113
|
||||||
local pcall = function(f, ...)
|
|
||||||
local n = select("#", ...);
|
|
||||||
local params = {...};
|
|
||||||
return xpcall(function() return f(unpack(params, 1, n)) end, function(e) return tostring(e).."\n"..debug_traceback(); end);
|
|
||||||
end
|
|
||||||
|
|
||||||
local autoload_modules = {prosody.platform, "presence", "message", "iq", "offline", "c2s", "s2s", "s2s_auth_certs"};
|
local autoload_modules = {prosody.platform, "presence", "message", "iq", "offline", "c2s", "s2s", "s2s_auth_certs"};
|
||||||
local component_inheritable_modules = {"tls", "saslauth", "dialback", "iq", "s2s"};
|
local component_inheritable_modules = {"tls", "saslauth", "dialback", "iq", "s2s"};
|
||||||
|
@ -183,7 +178,7 @@ local function do_load_module(host, module_name, state)
|
||||||
api_instance.path = err;
|
api_instance.path = err;
|
||||||
|
|
||||||
modulemap[host][module_name] = pluginenv;
|
modulemap[host][module_name] = pluginenv;
|
||||||
local ok, err = pcall(mod);
|
local ok, err = xpcall(mod, debug.traceback);
|
||||||
if ok then
|
if ok then
|
||||||
-- Call module's "load"
|
-- Call module's "load"
|
||||||
if module_has_method(pluginenv, "load") then
|
if module_has_method(pluginenv, "load") then
|
||||||
|
|
|
@ -20,8 +20,9 @@ local ssl_available = pcall(require, "ssl");
|
||||||
|
|
||||||
local t_insert, t_concat = table.insert, table.concat;
|
local t_insert, t_concat = table.insert, table.concat;
|
||||||
local pairs = pairs;
|
local pairs = pairs;
|
||||||
local tonumber, tostring, xpcall, traceback =
|
local tonumber, tostring, traceback =
|
||||||
tonumber, tostring, xpcall, debug.traceback;
|
tonumber, tostring, debug.traceback;
|
||||||
|
local xpcall = require "util.xpcall".xpcall;
|
||||||
local error = error
|
local error = error
|
||||||
local setmetatable = setmetatable;
|
local setmetatable = setmetatable;
|
||||||
|
|
||||||
|
@ -101,7 +102,7 @@ function listener.onconnect(conn)
|
||||||
end
|
end
|
||||||
|
|
||||||
log("debug", "Request '%s': Calling callback, status %s", req.id, code or "---");
|
log("debug", "Request '%s': Calling callback, status %s", req.id, code or "---");
|
||||||
return log_if_failed(req.id, xpcall(function () return callback(content, code, response, request) end, handleerr));
|
return log_if_failed(req.id, xpcall(callback, handleerr, content, code, response, request));
|
||||||
end
|
end
|
||||||
req.reader = request_reader;
|
req.reader = request_reader;
|
||||||
req.state = "status";
|
req.state = "status";
|
||||||
|
|
|
@ -8,7 +8,7 @@ local os_date = os.date;
|
||||||
local pairs = pairs;
|
local pairs = pairs;
|
||||||
local s_upper = string.upper;
|
local s_upper = string.upper;
|
||||||
local setmetatable = setmetatable;
|
local setmetatable = setmetatable;
|
||||||
local xpcall = xpcall;
|
local xpcall = require "util.xpcall".xpcall;
|
||||||
local traceback = debug.traceback;
|
local traceback = debug.traceback;
|
||||||
local tostring = tostring;
|
local tostring = tostring;
|
||||||
local cache = require "util.cache";
|
local cache = require "util.cache";
|
||||||
|
@ -88,8 +88,6 @@ setmetatable(events._handlers, {
|
||||||
});
|
});
|
||||||
|
|
||||||
local handle_request;
|
local handle_request;
|
||||||
local _1, _2, _3;
|
|
||||||
local function _handle_request() return handle_request(_1, _2, _3); end
|
|
||||||
|
|
||||||
local last_err;
|
local last_err;
|
||||||
local function _traceback_handler(err) last_err = err; log("error", "Traceback[httpserver]: %s", traceback(tostring(err), 2)); end
|
local function _traceback_handler(err) last_err = err; log("error", "Traceback[httpserver]: %s", traceback(tostring(err), 2)); end
|
||||||
|
@ -107,9 +105,7 @@ function listener.onconnect(conn)
|
||||||
while sessions[conn] and #pending > 0 do
|
while sessions[conn] and #pending > 0 do
|
||||||
local request = t_remove(pending);
|
local request = t_remove(pending);
|
||||||
--log("debug", "process_next: %s", request.path);
|
--log("debug", "process_next: %s", request.path);
|
||||||
--handle_request(conn, request, process_next);
|
if not xpcall(handle_request, _traceback_handler, conn, request, process_next) then
|
||||||
_1, _2, _3 = conn, request, process_next;
|
|
||||||
if not xpcall(_handle_request, _traceback_handler) then
|
|
||||||
conn:write("HTTP/1.0 500 Internal Server Error\r\n\r\n"..events.fire_event("http-error", { code = 500, private_message = last_err }));
|
conn:write("HTTP/1.0 500 Internal Server Error\r\n\r\n"..events.fire_event("http-error", { code = 500, private_message = last_err }));
|
||||||
conn:close();
|
conn:close();
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,7 +9,8 @@
|
||||||
module:set_global();
|
module:set_global();
|
||||||
|
|
||||||
local t_concat = table.concat;
|
local t_concat = table.concat;
|
||||||
local xpcall, tostring, type = xpcall, tostring, type;
|
local tostring, type = tostring, type;
|
||||||
|
local xpcall = require "util.xpcall".xpcall;
|
||||||
local traceback = debug.traceback;
|
local traceback = debug.traceback;
|
||||||
|
|
||||||
local logger = require "util.logger";
|
local logger = require "util.logger";
|
||||||
|
@ -238,7 +239,7 @@ function stream_callbacks.handlestanza(session, stanza)
|
||||||
end
|
end
|
||||||
|
|
||||||
if stanza then
|
if stanza then
|
||||||
return xpcall(function () return core_process_stanza(session, stanza) end, handleerr);
|
return xpcall(core_process_stanza, handleerr, session, stanza);
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
local logger = require "util.logger";
|
local logger = require "util.logger";
|
||||||
local log = logger.init("util.async");
|
local log = logger.init("util.async");
|
||||||
local new_id = require "util.id".short;
|
local new_id = require "util.id".short;
|
||||||
|
local xpcall = require "util.xpcall".xpcall;
|
||||||
|
|
||||||
local function checkthread()
|
local function checkthread()
|
||||||
local thread, main = coroutine.running();
|
local thread, main = coroutine.running();
|
||||||
|
@ -27,7 +28,7 @@ local function call_watcher(runner, watcher_name, ...)
|
||||||
return false;
|
return false;
|
||||||
end
|
end
|
||||||
runner:log("debug", "Calling '%s' watcher", watcher_name);
|
runner:log("debug", "Calling '%s' watcher", watcher_name);
|
||||||
local ok, err = pcall(watcher, runner, ...); -- COMPAT: Switch to xpcall after Lua 5.1
|
local ok, err = xpcall(watcher, debug.traceback, runner, ...);
|
||||||
if not ok then
|
if not ok then
|
||||||
runner:log("error", "Error in '%s' watcher: %s", watcher_name, err);
|
runner:log("error", "Error in '%s' watcher: %s", watcher_name, err);
|
||||||
return nil, err;
|
return nil, err;
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
local promise_methods = {};
|
local promise_methods = {};
|
||||||
local promise_mt = { __name = "promise", __index = promise_methods };
|
local promise_mt = { __name = "promise", __index = promise_methods };
|
||||||
|
|
||||||
|
local xpcall = require "util.xpcall".xpcall;
|
||||||
|
|
||||||
function promise_mt:__tostring()
|
function promise_mt:__tostring()
|
||||||
return "promise (" .. (self._state or "invalid") .. ")";
|
return "promise (" .. (self._state or "invalid") .. ")";
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,7 +13,7 @@ local get_time = require "util.time".now
|
||||||
local type = type;
|
local type = type;
|
||||||
local debug_traceback = debug.traceback;
|
local debug_traceback = debug.traceback;
|
||||||
local tostring = tostring;
|
local tostring = tostring;
|
||||||
local xpcall = xpcall;
|
local xpcall = require "util.xpcall".xpcall;
|
||||||
local math_max = math.max;
|
local math_max = math.max;
|
||||||
|
|
||||||
local _ENV = nil;
|
local _ENV = nil;
|
||||||
|
@ -26,24 +26,20 @@ local _active_timers = 0;
|
||||||
local h = indexedbheap.create();
|
local h = indexedbheap.create();
|
||||||
local params = {};
|
local params = {};
|
||||||
local next_time = nil;
|
local next_time = nil;
|
||||||
local _id, _callback, _now, _param;
|
|
||||||
local function _call() return _callback(_now, _id, _param); end
|
|
||||||
local function _traceback_handler(err) log("error", "Traceback[timer]: %s", debug_traceback(tostring(err), 2)); end
|
local function _traceback_handler(err) log("error", "Traceback[timer]: %s", debug_traceback(tostring(err), 2)); end
|
||||||
local function _on_timer(now)
|
local function _on_timer(now)
|
||||||
local peek;
|
local peek;
|
||||||
while true do
|
while true do
|
||||||
peek = h:peek();
|
peek = h:peek();
|
||||||
if peek == nil or peek > now then break; end
|
if peek == nil or peek > now then break; end
|
||||||
local _;
|
local _, callback, id = h:pop();
|
||||||
_, _callback, _id = h:pop();
|
local param = params[id];
|
||||||
_now = now;
|
params[id] = nil;
|
||||||
_param = params[_id];
|
--item(now, id, _param);
|
||||||
params[_id] = nil;
|
local success, err = xpcall(callback, _traceback_handler, now, id, param);
|
||||||
--item(now, id, _param); -- FIXME pcall
|
|
||||||
local success, err = xpcall(_call, _traceback_handler);
|
|
||||||
if success and type(err) == "number" then
|
if success and type(err) == "number" then
|
||||||
h:insert(_callback, err + now, _id); -- re-add
|
h:insert(callback, err + now, id); -- re-add
|
||||||
params[_id] = _param;
|
params[id] = param;
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue