Skip to content

Commit 8040ea2

Browse files
committed
Improve error logging further
I relearned how to use xpcall, yay.
1 parent 491e4dc commit 8040ea2

File tree

3 files changed

+60
-48
lines changed

3 files changed

+60
-48
lines changed

client.lua

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ if OUTPUT then
3030
end
3131
end
3232

33-
local env = setmetatable({}, { __index = function(_, key)
34-
return rawget(_G, key) or error("__index on env: " .. tostring(key), 2)
33+
local env__ = setmetatable({}, { __index = function(_, key)
34+
error("__index on env: " .. tostring(key), 2)
3535
end, __newindex = function(_, key)
3636
error("__newindex on env: " .. tostring(key), 2)
3737
end })
38-
local _ENV = env
38+
for key, value in pairs(_G) do
39+
rawset(env__, key, value)
40+
end
41+
local _ENV = env__
3942
if rawget(_G, "setfenv") then
40-
setfenv(1, env)
43+
setfenv(1, env__)
4144
end
4245

4346
math.randomseed(os.time())
@@ -77,8 +80,8 @@ local function xpcall_wrap(func, handler)
7780
if handler then
7881
handler(err)
7982
end
80-
print(err)
81-
print(debug.traceback())
83+
print(debug.traceback(err, 2))
84+
return err
8285
end)
8386
if oargs then
8487
return unpackn(oargs)
@@ -105,13 +108,13 @@ local function require(modname)
105108
if rawget(_G, "setfenv") then
106109
func, err = loadstring(content, "=" .. relative)
107110
else
108-
func, err = load(content, "=" .. relative, "bt", env)
111+
func, err = load(content, "=" .. relative, "bt", env__)
109112
end
110113
if not func then
111114
error(err, 0)
112115
end
113116
if rawget(_G, "setfenv") then
114-
setfenv(func, env)
117+
setfenv(func, env__)
115118
end
116119
local ok = true
117120
local err_outer
@@ -135,18 +138,21 @@ local function require(modname)
135138
end
136139
return loaded[modname]
137140
end
138-
rawset(env, "require", require)
139-
rawset(env, "xpcall_wrap", xpcall_wrap)
141+
rawset(env__, "require", require)
142+
rawset(env__, "xpcall_wrap", xpcall_wrap)
140143

141144
local main_module = require(MAIN_MODULE)
142145
if OUTPUT then
143146
local handle = assert(io.open(OUTPUT, "w"))
144147
handle:write([[
145148
local env__ = setmetatable({}, { __index = function(_, key)
146-
return rawget(_G, key) or error("__index on env: " .. tostring(key), 2)
149+
error("__index on env: " .. tostring(key), 2)
147150
end, __newindex = function(_, key)
148151
error("__newindex on env: " .. tostring(key), 2)
149152
end })
153+
for key, value in pairs(_G) do
154+
rawset(env__, key, value)
155+
end
150156
local _ENV = env__
151157
if rawget(_G, "setfenv") then
152158
setfenv(1, env__)
@@ -183,8 +189,8 @@ local function xpcall_wrap(func, handler)
183189
if handler then
184190
handler(err)
185191
end
186-
print(err)
187-
print(debug.traceback())
192+
print(debug.traceback(err, 2))
193+
return err
188194
end)
189195
if oargs then
190196
return unpackn(oargs)

tptmp/client/client.lua

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,10 @@ function client_i:start()
10591059
handler(self)
10601060
end
10611061
end, function(err)
1062-
print(debug.traceback(err, 2))
1062+
if self.handle_error_func_ then
1063+
self.handle_error_func_(err)
1064+
end
1065+
return err
10631066
end)
10641067
if not ok then
10651068
error(err)
@@ -1105,7 +1108,7 @@ function client_i:tick_resume_()
11051108
local ok, err = coroutine.resume(self.proto_coro_)
11061109
if not ok then
11071110
self.proto_coro_ = nil
1108-
error(err)
1111+
error("proto coroutine: " .. err, 0)
11091112
end
11101113
if self.proto_coro_ and coroutine.status(self.proto_coro_) == "dead" then
11111114
error("proto coroutine terminated")
@@ -1414,31 +1417,32 @@ end
14141417
local function new(params)
14151418
local now = socket.gettime()
14161419
return setmetatable({
1417-
host_ = params.host,
1418-
port_ = params.port,
1419-
secure_ = params.secure,
1420-
event_log_ = params.event_log,
1421-
backlog_ = params.backlog,
1422-
rx_ = buffer_list.new({ limit = config.recvq_limit }),
1423-
tx_ = buffer_list.new({ limit = config.sendq_limit }),
1424-
connecting_since_ = now,
1425-
last_ping_sent_at_ = now,
1426-
last_ping_received_at_ = now,
1427-
status_ = "ready",
1428-
window_ = params.window,
1429-
profile_ = params.profile,
1430-
localcmd_ = params.localcmd,
1431-
initial_room_ = params.initial_room,
1432-
set_id_func_ = params.set_id_func,
1433-
get_id_func_ = params.get_id_func,
1434-
set_qa_func_ = params.set_qa_func,
1435-
get_qa_func_ = params.get_qa_func,
1436-
log_event_func_ = params.log_event_func,
1437-
should_reconnect_func_ = params.should_reconnect_func,
1420+
host_ = params.host,
1421+
port_ = params.port,
1422+
secure_ = params.secure,
1423+
event_log_ = params.event_log,
1424+
backlog_ = params.backlog,
1425+
rx_ = buffer_list.new({ limit = config.recvq_limit }),
1426+
tx_ = buffer_list.new({ limit = config.sendq_limit }),
1427+
connecting_since_ = now,
1428+
last_ping_sent_at_ = now,
1429+
last_ping_received_at_ = now,
1430+
status_ = "ready",
1431+
window_ = params.window,
1432+
profile_ = params.profile,
1433+
localcmd_ = params.localcmd,
1434+
initial_room_ = params.initial_room,
1435+
set_id_func_ = params.set_id_func,
1436+
get_id_func_ = params.get_id_func,
1437+
set_qa_func_ = params.set_qa_func,
1438+
get_qa_func_ = params.get_qa_func,
1439+
log_event_func_ = params.log_event_func,
1440+
handle_error_func_ = params.handle_error_func,
1441+
should_reconnect_func_ = params.should_reconnect_func,
14381442
should_not_reconnect_func_ = params.should_not_reconnect_func,
1439-
id_to_member = {},
1440-
nick_colour_seed_ = 0,
1441-
fps_sync_ = false,
1443+
id_to_member = {},
1444+
nick_colour_seed_ = 0,
1445+
fps_sync_ = false,
14421446
}, client_m)
14431447
end
14441448

tptmp/client/init.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ local function run()
101101
end
102102

103103
local last_trace_str
104+
local handle_error
104105

105106
local should_reconnect_at
106107
local cli
@@ -163,13 +164,14 @@ local function run()
163164
end,
164165
new_client_func = function(params)
165166
should_reconnect_at = nil
166-
params.window = win
167-
params.profile = prof
168-
params.set_id_func = set_id
169-
params.get_id_func = get_id
170-
params.set_qa_func = set_qa
171-
params.get_qa_func = get_qa
172-
params.log_event_func = log_event
167+
params.window = win
168+
params.profile = prof
169+
params.set_id_func = set_id
170+
params.get_id_func = get_id
171+
params.set_qa_func = set_qa
172+
params.get_qa_func = get_qa
173+
params.log_event_func = log_event
174+
params.handle_error_func = handle_error
173175
params.should_reconnect_func = function()
174176
should_reconnect = true
175177
end
@@ -218,15 +220,15 @@ local function run()
218220
end
219221
end
220222

221-
local function handle_error(err)
223+
function handle_error(err)
222224
if not last_trace_str then
223225
local handle = io.open(config.trace_path, "wb")
224226
handle:write(("TPTMP %s %s\n"):format(config.versionstr, os.date("!%Y-%m-%dT%H:%M:%SZ")))
225227
handle:close()
226228
win:backlog_push_error("An error occurred and its trace has been saved to " .. config.trace_path .. "; please find this file in your data folder and attach it when reporting this to developers")
227229
win:backlog_push_error("Top-level error: " .. tostring(err))
228230
end
229-
local str = tostring(err) .. "\n" .. debug.traceback() .. "\n"
231+
local str = debug.traceback(err, 2) .. "\n"
230232
if last_trace_str ~= str then
231233
last_trace_str = str
232234
local handle = io.open(config.trace_path, "ab")

0 commit comments

Comments
 (0)