diff --git a/features/server.lua b/features/server.lua index 8f9d08c84..deeccb013 100644 --- a/features/server.lua +++ b/features/server.lua @@ -14,6 +14,7 @@ local tostring = tostring local raw_print = Print.raw_print local next = next local type = type +local xpcall = xpcall local serialize_options = {sparse = true, compact = true} @@ -532,19 +533,16 @@ local function data_set_changed(data) if _DEBUG then for _, handler in ipairs(handlers) do - local success, err = pcall(handler, data) + local success, result = xpcall(handler, ErrorLogging.error_handler, data) if not success then - log(err) - ErrorLogging.generate_error_report(err) - error(err, 2) + log(result) end end else for _, handler in ipairs(handlers) do - local success, err = pcall(handler, data) + local success, result = xpcall(handler, ErrorLogging.error_handler, data) if not success then - log(err) - ErrorLogging.generate_error_report(err) + log(result) end end end diff --git a/utils/command.lua b/utils/command.lua index b03f3bc51..c3fecd307 100644 --- a/utils/command.lua +++ b/utils/command.lua @@ -15,7 +15,7 @@ local serialize = serpent.line local gmatch = string.gmatch local get_rank_name = Rank.get_rank_name local pairs = pairs -local pcall = pcall +local xpcall = xpcall local Command = {} @@ -250,26 +250,24 @@ function Command.add(command_name, options, callback) log({'command.log_entry', server_time, tick, (required_rank >= Ranks.admin) and 'Admin' or 'Player', player_name, command_name, serialize(named_arguments)}) end - local success, error = - pcall( + local success, result = xpcall( function() callback(named_arguments, player, command.tick) - end + end, + ErrorLogging.error_handler ) if not success then local serialized_arguments = serialize(named_arguments) if _DEBUG then print({'command.error_while_running_debug', player_name, command_name, serialized_arguments}) - print(error) - ErrorLogging.generate_error_report(error) + print(result) return end print({'command.warn_player_of_error', command_name}) - local err = {'command.error_log', command_name, serialized_arguments, error} + local err = {'command.error_log', command_name, serialized_arguments, result} log(err) - ErrorLogging.generate_error_report(err) end end ) diff --git a/utils/error_logging.lua b/utils/error_logging.lua index 32e618afd..ef4acdad2 100644 --- a/utils/error_logging.lua +++ b/utils/error_logging.lua @@ -10,7 +10,8 @@ local floor = math.floor local format = string.format local insert = table.insert local concat = table.concat -local pcall = pcall +local xpcall = xpcall +local trace = debug.traceback -- Local constants local minutes_to_ticks = 60 * 60 @@ -85,10 +86,14 @@ end --- Takes the given string and generates an entry in the error file. function Public.generate_error_report(str) - local success, err = pcall(try_generate_report, str) + local success, result = xpcall(try_generate_report, Public.error_handler, str) if not success then - log(err) + log(result) end end +function Public.error_handler(err) + return trace(err) +end + return Public diff --git a/utils/event_core.lua b/utils/event_core.lua index 5ce10fc97..fbd132f0d 100644 --- a/utils/event_core.lua +++ b/utils/event_core.lua @@ -14,10 +14,11 @@ local event_handlers = {} -- map of nth_tick to handlers[] local on_nth_tick_event_handlers = {} -local pcall = pcall +local xpcall = xpcall local log = log local script_on_event = script.on_event local script_on_nth_tick = script.on_nth_tick +local error_handler = ErrorLogging.error_handler local call_handlers if _DEBUG then @@ -31,10 +32,10 @@ else function call_handlers(handlers, event) for i = #handlers, 1, -1 do local handler = handlers[i] - local success, error = pcall(handler, event) + local success, result = xpcall(handler, error_handler, event) + if not success then - log(error) - ErrorLogging.generate_error_report(error) + log(result) end end end diff --git a/utils/task.lua b/utils/task.lua index 7497a29e7..a67b0b963 100644 --- a/utils/task.lua +++ b/utils/task.lua @@ -14,13 +14,14 @@ local Global = require 'utils.global' local floor = math.floor local log10 = math.log10 local Token_get = Token.get -local pcall = pcall +local xpcall = xpcall local Queue_peek = Queue.peek local Queue_pop = Queue.pop local Queue_push = Queue.push local PriorityQueue_peek = PriorityQueue.peek local PriorityQueue_pop = PriorityQueue.pop local PriorityQueue_push = PriorityQueue.push +local error_handler = ErrorLogging.error_handler local Task = {} @@ -69,13 +70,12 @@ local function on_tick() local task = Queue_peek(task_queue) if task ~= nil then -- result is error if not success else result is a boolean for if the task should stay in the queue. - local success, result = pcall(Token_get(task.func_token), task.params) + local success, result = xpcall(Token_get(task.func_token), error_handler, task.params) if not success then if _DEBUG then error(result) else log(result) - ErrorLogging.generate_error_report(result) end Queue_pop(task_queue) primitives.total_task_weight = primitives.total_task_weight - task.weight @@ -88,13 +88,12 @@ local function on_tick() local callback = PriorityQueue_peek(callbacks) while callback ~= nil and tick >= callback.time do - local success, result = pcall(Token_get(callback.func_token), callback.params) + local success, result = xpcall(Token_get(callback.func_token), error_handler, callback.params) if not success then if _DEBUG then error(result) else log(result) - ErrorLogging.generate_error_report(result) end end PriorityQueue_pop(callbacks)