Skip to content

Commit acb7b9d

Browse files
committed
Update error handler
1 parent 08891f8 commit acb7b9d

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
lines changed

features/server.lua

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ local tostring = tostring
1414
local raw_print = Print.raw_print
1515
local next = next
1616
local type = type
17+
local xpcall = xpcall
1718

1819
local serialize_options = {sparse = true, compact = true}
1920

@@ -532,19 +533,16 @@ local function data_set_changed(data)
532533

533534
if _DEBUG then
534535
for _, handler in ipairs(handlers) do
535-
local success, err = pcall(handler, data)
536+
local success, result = xpcall(handler, ErrorLogging.error_handler, data)
536537
if not success then
537-
log(err)
538-
ErrorLogging.generate_error_report(err)
539-
error(err, 2)
538+
log(result)
540539
end
541540
end
542541
else
543542
for _, handler in ipairs(handlers) do
544-
local success, err = pcall(handler, data)
543+
local success, result = xpcall(handler, ErrorLogging.error_handler, data)
545544
if not success then
546-
log(err)
547-
ErrorLogging.generate_error_report(err)
545+
log(result)
548546
end
549547
end
550548
end

utils/command.lua

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ local serialize = serpent.line
1515
local gmatch = string.gmatch
1616
local get_rank_name = Rank.get_rank_name
1717
local pairs = pairs
18-
local pcall = pcall
18+
local xpcall = xpcall
1919

2020
local Command = {}
2121

@@ -250,26 +250,24 @@ function Command.add(command_name, options, callback)
250250
log({'command.log_entry', server_time, tick, (required_rank >= Ranks.admin) and 'Admin' or 'Player', player_name, command_name, serialize(named_arguments)})
251251
end
252252

253-
local success, error =
254-
pcall(
253+
local success, result = xpcall(
255254
function()
256255
callback(named_arguments, player, command.tick)
257-
end
256+
end,
257+
ErrorLogging.error_handler
258258
)
259259

260260
if not success then
261261
local serialized_arguments = serialize(named_arguments)
262262
if _DEBUG then
263263
print({'command.error_while_running_debug', player_name, command_name, serialized_arguments})
264-
print(error)
265-
ErrorLogging.generate_error_report(error)
264+
print(result)
266265
return
267266
end
268267

269268
print({'command.warn_player_of_error', command_name})
270-
local err = {'command.error_log', command_name, serialized_arguments, error}
269+
local err = {'command.error_log', command_name, serialized_arguments, result}
271270
log(err)
272-
ErrorLogging.generate_error_report(err)
273271
end
274272
end
275273
)

utils/error_logging.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ local floor = math.floor
1010
local format = string.format
1111
local insert = table.insert
1212
local concat = table.concat
13-
local pcall = pcall
13+
local xpcall = xpcall
14+
local trace = debug.traceback
1415

1516
-- Local constants
1617
local minutes_to_ticks = 60 * 60
@@ -85,10 +86,14 @@ end
8586

8687
--- Takes the given string and generates an entry in the error file.
8788
function Public.generate_error_report(str)
88-
local success, err = pcall(try_generate_report, str)
89+
local success, result = xpcall(try_generate_report, Public.error_handler, str)
8990
if not success then
90-
log(err)
91+
log(result)
9192
end
9293
end
9394

95+
function Public.error_handler(err)
96+
return trace(err)
97+
end
98+
9499
return Public

utils/event_core.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ local event_handlers = {}
1414
-- map of nth_tick to handlers[]
1515
local on_nth_tick_event_handlers = {}
1616

17-
local pcall = pcall
17+
local xpcall = xpcall
1818
local log = log
1919
local script_on_event = script.on_event
2020
local script_on_nth_tick = script.on_nth_tick
21+
local error_handler = ErrorLogging.error_handler
2122

2223
local call_handlers
2324
if _DEBUG then
@@ -31,10 +32,10 @@ else
3132
function call_handlers(handlers, event)
3233
for i = #handlers, 1, -1 do
3334
local handler = handlers[i]
34-
local success, error = pcall(handler, event)
35+
local success, result = xpcall(handler, error_handler, event)
36+
3537
if not success then
36-
log(error)
37-
ErrorLogging.generate_error_report(error)
38+
log(result)
3839
end
3940
end
4041
end

utils/task.lua

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ local Global = require 'utils.global'
1414
local floor = math.floor
1515
local log10 = math.log10
1616
local Token_get = Token.get
17-
local pcall = pcall
17+
local xpcall = xpcall
1818
local Queue_peek = Queue.peek
1919
local Queue_pop = Queue.pop
2020
local Queue_push = Queue.push
2121
local PriorityQueue_peek = PriorityQueue.peek
2222
local PriorityQueue_pop = PriorityQueue.pop
2323
local PriorityQueue_push = PriorityQueue.push
24+
local error_handler = ErrorLogging.error_handler
2425

2526
local Task = {}
2627

@@ -69,13 +70,12 @@ local function on_tick()
6970
local task = Queue_peek(task_queue)
7071
if task ~= nil then
7172
-- result is error if not success else result is a boolean for if the task should stay in the queue.
72-
local success, result = pcall(Token_get(task.func_token), task.params)
73+
local success, result = xpcall(Token_get(task.func_token), error_handler, task.params)
7374
if not success then
7475
if _DEBUG then
7576
error(result)
7677
else
7778
log(result)
78-
ErrorLogging.generate_error_report(result)
7979
end
8080
Queue_pop(task_queue)
8181
primitives.total_task_weight = primitives.total_task_weight - task.weight
@@ -88,13 +88,12 @@ local function on_tick()
8888

8989
local callback = PriorityQueue_peek(callbacks)
9090
while callback ~= nil and tick >= callback.time do
91-
local success, result = pcall(Token_get(callback.func_token), callback.params)
91+
local success, result = xpcall(Token_get(callback.func_token), error_handler, callback.params)
9292
if not success then
9393
if _DEBUG then
9494
error(result)
9595
else
9696
log(result)
97-
ErrorLogging.generate_error_report(result)
9897
end
9998
end
10099
PriorityQueue_pop(callbacks)

0 commit comments

Comments
 (0)