2016-02-28 12:07:02 +01:00
|
|
|
---------------------------------------------------------------------------
|
2021-12-21 06:54:15 +01:00
|
|
|
-- Safely call a function and handle errors using `gears.debug`.
|
|
|
|
--
|
|
|
|
-- This is a `pcall`/`xpcall` wrapper. All it does is to integrate into the
|
|
|
|
-- AwesomeWM-wide error handling and logging.
|
|
|
|
--
|
2016-02-28 12:07:02 +01:00
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2016 Uli Schlachter
|
2019-06-06 09:40:17 +02:00
|
|
|
-- @utillib gears.protected_call
|
2016-02-28 12:07:02 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local gdebug = require("gears.debug")
|
|
|
|
local tostring = tostring
|
|
|
|
local traceback = debug.traceback
|
|
|
|
local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1)
|
|
|
|
local xpcall = xpcall
|
|
|
|
|
|
|
|
local protected_call = {}
|
|
|
|
|
2019-07-22 06:22:20 +02:00
|
|
|
function protected_call._error_handler(err)
|
2016-10-22 00:54:08 +02:00
|
|
|
gdebug.print_error(traceback("Error during a protected call: " .. tostring(err), 2))
|
2016-02-28 12:07:02 +01:00
|
|
|
end
|
|
|
|
|
2019-07-22 06:22:20 +02:00
|
|
|
function protected_call._handle_result(success, ...)
|
2016-02-28 12:07:02 +01:00
|
|
|
if success then
|
|
|
|
return ...
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local do_pcall
|
2019-02-15 09:25:06 +01:00
|
|
|
if not select(2, xpcall(function(a) return a end, error, true)) then
|
2016-02-28 12:07:02 +01:00
|
|
|
-- Lua 5.1 doesn't support arguments in xpcall :-(
|
|
|
|
do_pcall = function(func, ...)
|
|
|
|
local args = { ... }
|
2019-07-22 06:22:20 +02:00
|
|
|
return protected_call._handle_result(xpcall(function()
|
2016-02-28 12:07:02 +01:00
|
|
|
return func(unpack(args))
|
2019-07-22 06:22:20 +02:00
|
|
|
end, protected_call._error_handler))
|
2016-02-28 12:07:02 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
do_pcall = function(func, ...)
|
2019-07-22 06:22:20 +02:00
|
|
|
return protected_call._handle_result(xpcall(func, protected_call._error_handler, ...))
|
2016-02-28 12:07:02 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Call a function in protected mode and handle error-reporting.
|
|
|
|
-- If the function call succeeds, all results of the function are returned.
|
|
|
|
-- Otherwise, an error message is printed and nothing is returned.
|
|
|
|
-- @tparam function func The function to call
|
|
|
|
-- @param ... Arguments to the function
|
|
|
|
-- @return The result of the given function, or nothing if an error occurred.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct gears.protected_call
|
2016-02-28 12:07:02 +01:00
|
|
|
function protected_call.call(func, ...)
|
|
|
|
return do_pcall(func, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
local pcall_mt = {}
|
|
|
|
function pcall_mt:__call(...)
|
|
|
|
return do_pcall(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(protected_call, pcall_mt)
|
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|