Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions features/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions utils/command.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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
)
Expand Down
11 changes: 8 additions & 3 deletions utils/error_logging.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does trace do in factorio? Does it go into the logs? If so do we need both trace and log(result) in event_core and others?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lua 5.2 debug::traceback it only returns a string with a traceback of the call stack, which is then logged by the log function

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that makes sense.

end

return Public
9 changes: 5 additions & 4 deletions utils/event_core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions utils/task.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down