From 44587cec359415e7891fffe2f0297d3c5b92b9b6 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Tue, 13 Mar 2018 03:51:11 +0100 Subject: [PATCH] awful.remote: Run code in a protected context (#2203) This change makes errors messages show up in the replies, too. Before (notice how the third command does not generate output): $ awesome-client 'return 42' ; awesome-client 'sdfsdf' ; awesome-client 'error("foo")' double 42 string "[string "sdfsdf"]:1: syntax error near " After: $ awesome-client 'return 42' ; awesome-client 'sdfsdf' ; awesome-client 'error("foo")' double 42 string "[string "sdfsdf"]:1: syntax error near " string "Error during execution: [string "error("foo")"]:1: foo" Signed-off-by: Uli Schlachter --- lib/awful/remote.lua | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/awful/remote.lua b/lib/awful/remote.lua index 7967f2fb..f419ffef 100644 --- a/lib/awful/remote.lua +++ b/lib/awful/remote.lua @@ -20,26 +20,28 @@ if dbus then dbus.connect_signal("org.awesomewm.awful.Remote", function(data, code) if data.member == "Eval" then local f, e = load(code) - if f then - local results = { f() } - local retvals = {} - for _, v in ipairs(results) do - local t = type(v) - if t == "boolean" then - table.insert(retvals, "b") - table.insert(retvals, v) - elseif t == "number" then - table.insert(retvals, "d") - table.insert(retvals, v) - else - table.insert(retvals, "s") - table.insert(retvals, tostring(v)) - end - end - return unpack(retvals) - elseif e then + if not f then return "s", e end + local results = { pcall(f) } + if not table.remove(results, 1) then + return "s", "Error during execution: " .. results[1] + end + local retvals = {} + for _, v in ipairs(results) do + local t = type(v) + if t == "boolean" then + table.insert(retvals, "b") + table.insert(retvals, v) + elseif t == "number" then + table.insert(retvals, "d") + table.insert(retvals, v) + else + table.insert(retvals, "s") + table.insert(retvals, tostring(v)) + end + end + return unpack(retvals) end end) end