From 8560c6d2af9c6d8747b0f0df587ad6bb97633921 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 28 Feb 2016 12:28:55 +0100 Subject: [PATCH] Use gears.protected_call where applicable Signed-off-by: Uli Schlachter --- lib/awful/menu.lua | 10 +++------- lib/awful/spawn.lua | 17 +++++------------ lib/beautiful/init.lua | 10 +++------- lib/gears/timer.lua | 19 +++++-------------- lib/wibox/hierarchy.lua | 12 +++--------- 5 files changed, 19 insertions(+), 49 deletions(-) diff --git a/lib/awful/menu.lua b/lib/awful/menu.lua index 2e29179df..8fdd51cb2 100644 --- a/lib/awful/menu.lua +++ b/lib/awful/menu.lua @@ -20,13 +20,13 @@ local beautiful = require("beautiful") local dpi = require("beautiful").xresources.apply_dpi local object = require("gears.object") local surface = require("gears.surface") +local protected_call = require("gears.protected_call") local cairo = require("lgi").cairo local setmetatable = setmetatable local tonumber = tonumber local string = string local ipairs = ipairs local pairs = pairs -local pcall = pcall local print = print local table = table local type = type @@ -374,12 +374,8 @@ function menu:add(args, index) local theme = load_theme(args.theme or {}, self.theme) args.theme = theme args.new = args.new or menu.entry - local success, item = pcall(args.new, self, args) - if not success then - print("Error while creating menu entry: " .. item) - return - end - if not item.widget then + local item = protected_call(args.new, self, args) + if (not item) or (not item.widget) then print("Error while checking menu entry: no property widget found.") return end diff --git a/lib/awful/spawn.lua b/lib/awful/spawn.lua index 395f666a3..b25ca803d 100644 --- a/lib/awful/spawn.lua +++ b/lib/awful/spawn.lua @@ -24,6 +24,7 @@ local lgi = require("lgi") local Gio = lgi.Gio local GLib = lgi.GLib local util = require("awful.util") +local protected_call = require("gears.protected_call") local spawn = {} @@ -205,10 +206,7 @@ function spawn.read_lines(input_stream, line_callback, done_callback, close) stream:close() end if done_callback then - xpcall(done_callback, function(err) - print(debug.traceback("Error while calling done_callback:" - .. tostring(err), 2)) - end) + protected_call(done_callback) end end local start_read, finish_read @@ -226,14 +224,9 @@ function spawn.read_lines(input_stream, line_callback, done_callback, close) done() else -- Read a line - xpcall(function() - -- This needs tostring() for older lgi versions which returned - -- "GLib.Bytes" instead of Lua strings (I guess) - line_callback(tostring(line)) - end, function(err) - print(debug.traceback("Error while calling line_callback: " - .. tostring(err), 2)) - end) + -- This needs tostring() for older lgi versions which returned + -- "GLib.Bytes" instead of Lua strings (I guess) + protected_call(line_callback, tostring(line)) -- Read the next line start_read() diff --git a/lib/beautiful/init.lua b/lib/beautiful/init.lua index 7725f310c..d7f95f2f0 100644 --- a/lib/beautiful/init.lua +++ b/lib/beautiful/init.lua @@ -18,6 +18,7 @@ local lgi = require("lgi") local Pango = lgi.Pango local PangoCairo = lgi.PangoCairo local gears_debug = require("gears.debug") +local protected_call = require("gears.protected_call") local xresources = require("beautiful.xresources") @@ -109,7 +110,6 @@ end -- containing all the theme values. function beautiful.init(config) if config then - local success local homedir = os.getenv("HOME") -- If config is the path to the theme file, @@ -118,16 +118,12 @@ function beautiful.init(config) if type(config) == 'string' then -- Expand the '~' $HOME shortcut config = config:gsub("^~/", homedir .. "/") - success, theme = xpcall(function() return dofile(config) end, - debug.traceback) + theme = protected_call(dofile, config) elseif type(config) == 'table' then - success = true theme = config end - if not success then - return gears_debug.print_error("beautiful: error loading theme file " .. theme) - elseif theme then + if theme then -- expand '~' if homedir then for k, v in pairs(theme) do diff --git a/lib/gears/timer.lua b/lib/gears/timer.lua index 1c50cefe1..7c320ef2f 100644 --- a/lib/gears/timer.lua +++ b/lib/gears/timer.lua @@ -17,6 +17,7 @@ local traceback = debug.traceback local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1) local glib = require("lgi").GLib local object = require("gears.object") +local protected_call = require("gears.protected_call") --- Timer objects. This type of object is useful when triggering events repeatedly. -- The timer will emit the "timeout" signal every N seconds, N being the timeout @@ -43,11 +44,7 @@ function timer:start() return end self.data.source_id = glib.timeout_add(glib.PRIORITY_DEFAULT, self.data.timeout * 1000, function() - xpcall(function() - self:emit_signal("timeout") - end, function(err) - print(debug.traceback("Error during executing timeout handler: "..tostring(err))) - end) + protected_call(self.emit_signal, self, "timeout") return true end) self:emit_signal("start") @@ -126,10 +123,8 @@ end function timer.start_new(timeout, callback) local t = timer.new({ timeout = timeout }) t:connect_signal("timeout", function() - local success, cont = xpcall(callback, function(err) - print(debug.traceback("Error during executing timeout handler: "..tostring(err), 2)) - end) - if not success or not cont then + local cont = protected_call(callback) + if not cont then t:stop() end end) @@ -160,11 +155,7 @@ end local delayed_calls = {} capi.awesome.connect_signal("refresh", function() for _, callback in ipairs(delayed_calls) do - xpcall(function() - callback[1](unpack(callback, 2)) - end, function(err) - print(debug.traceback("Error during delayed call: "..tostring(err), 2)) - end) + protected_call(unpack(callback)) end delayed_calls = {} end) diff --git a/lib/wibox/hierarchy.lua b/lib/wibox/hierarchy.lua index 502d912fc..5e6abc471 100644 --- a/lib/wibox/hierarchy.lua +++ b/lib/wibox/hierarchy.lua @@ -10,6 +10,7 @@ --------------------------------------------------------------------------- local matrix = require("gears.matrix") +local protected_call = require("gears.protected_call") local cairo = require("lgi").cairo local base = require("wibox.widget.base") local no_parent = base.no_parent_I_know_what_I_am_doing @@ -274,17 +275,10 @@ function hierarchy:draw(context, cr) local opacity = widget.opacity local function call(func, extra_arg1, extra_arg2) if not func then return end - local function error_function(err) - print(debug.traceback("Error while drawing widget: " .. tostring(err), 2)) - end if not extra_arg2 then - xpcall(function() - func(widget, context, cr, self:get_size()) - end, error_function) + protected_call(func, widget, context, cr, self:get_size()) else - xpcall(function() - func(widget, context, extra_arg1, extra_arg2, cr, self:get_size()) - end, error_function) + protected_call(func, widget, context, extra_arg1, extra_arg2, cr, self:get_size()) end end