Quick answer: Defold crash reporting has two halves. Lua errors go through sys.set_error_handler(function(source, message, traceback) ... end), where you queue each distinct message once. Native crashes are read on the next start with crash.load_previous(), which returns a handle to the previous run’s dump or nil. Send both with http.request and json.encode from a script’s update, not from inside the handler.
A Lua error in a Defold game does not close the game. The engine logs ERROR:SCRIPT: main/enemy.script:42: attempt to index local 'target' (a nil value), the callback that failed is abandoned, and the player carries on in a game where enemies have stopped moving. Nobody reports that as a crash because nothing crashed. A real native crash is the opposite problem: the process is gone before any Lua can run. Defold has a hook for each case. This guide puts both behind one module that reports to a server and adds a simple way for players to report a problem themselves.
Two kinds of failure in a Defold game
| Failure | What the player sees | Hook | When your code runs |
|---|---|---|---|
| Lua error in a script, GUI script or module | The game keeps running in a broken state | sys.set_error_handler | Immediately, in the same session |
| Native crash in the engine, a driver or a native extension | The game closes | crash.load_previous | On the next start |
Most reports you will ever receive are the first kind. They are also the kind you would otherwise never hear about, because from the player’s side the game merely “got weird”.
A reporting module with sys.set_error_handler
The handler receives three strings. source is currently always "lua". message holds the file, line number and error text. traceback is the Lua stack. The handler should do as little as possible: remember the error and return. The actual request is made later from a script, which is a context where http.request and its callback are known to behave.
Create main/bugreport.lua. The example posts to Bugnet’s report endpoint; change the URL and fields for a different backend.
local M = {}
local URL = "https://api.bugnet.io/api/bugs/submit"
local API_KEY = "sk_live_your_project_key"
local MAX_AUTO_REPORTS = 5
local queue = {}
local seen = {}
local seen_count = 0
M.last_error = ""
M.level = "boot"
local function base_fields()
local info = sys.get_sys_info()
local engine = sys.get_engine_info()
local device = info.device_model or ""
if info.manufacturer and info.manufacturer ~= "" then
device = info.manufacturer .. " " .. device
end
return {
platform = info.system_name,
os_info = info.system_name .. " " .. info.system_version,
device_info = device,
game_version = sys.get_config_string("project.version", "0"),
metadata = {
defold = engine.version,
debug_build = engine.is_debug,
language = info.language,
level = M.level,
},
}
end
function M.send(report, on_done)
local body = base_fields()
for key, value in pairs(report) do
body[key] = value
end
local headers = {
["Content-Type"] = "application/json",
["X-API-Key"] = API_KEY,
}
http.request(URL, "POST", function(self, id, response)
local ok = response.status == 200 or response.status == 201
if not ok then
print("Bug report not sent, status " .. tostring(response.status))
end
if on_done then
on_done(ok)
end
end, headers, json.encode(body))
end
function M.install()
sys.set_error_handler(function(source, message, traceback)
M.last_error = message
if seen[message] or seen_count >= MAX_AUTO_REPORTS then
return
end
seen[message] = true
seen_count = seen_count + 1
queue[#queue + 1] = {
title = message:sub(1, 200),
description = message .. "\n\n" .. traceback,
priority = "high",
auto_captured = true,
}
end)
end
function M.set_level(name)
M.level = name
if crash then
crash.set_user_field(0, name)
end
end
function M.flush()
if #queue > 0 then
M.send(table.remove(queue, 1))
end
end
Three things in there are doing real work.
The seen table. An error in update is raised again on the next frame, and the frame after that. Without deduplication one nil reference sends sixty reports a second until the player quits. Each distinct message is queued once, and the session stops reporting after five different errors, because by then the game state is too far gone for the sixth to mean much.
The title is the message. main/enemy.script:42: attempt to index local 'target' (a nil value) is the same string for every player on the same build. A tracker that groups reports by title stacks them into one report with a count, which is exactly the number you want: how many players hit line 42. If your own error() calls put ids or coordinates in the message, move those into the description so the title stays constant.
project.version. sys.get_config_string("project.version", "0") reads the version field from game.project. Bump it for every release so each report says which build it came from.
Native crashes: crash.load_previous on the next start
When the engine itself dies, Defold’s crash handler writes a small dump file. Nothing can be sent at that moment. On the next launch, crash.load_previous() returns a handle to that dump, or nil if the last run ended normally. Loading the dump also removes it from disk, so each crash is seen once.
Add this to the module, before the final return M:
function M.check_previous_crash()
if not crash then
return
end
local handle = crash.load_previous()
if not handle then
return
end
local signum = tostring(crash.get_signum(handle))
local engine = tostring(crash.get_sys_field(handle, crash.SYSFIELD_ENGINE_VERSION))
local system = tostring(crash.get_sys_field(handle, crash.SYSFIELD_SYSTEM_NAME))
local level = tostring(crash.get_user_field(handle, 0))
local version = crash.get_user_field(handle, 1)
local lines = {
"Signal: " .. signum,
"Engine: " .. engine,
"System: " .. system .. " "
.. tostring(crash.get_sys_field(handle, crash.SYSFIELD_SYSTEM_VERSION)),
"Level: " .. level,
"",
tostring(crash.get_extra_data(handle)),
"",
"Backtrace addresses:",
}
for i, address in ipairs(crash.get_backtrace(handle)) do
lines[#lines + 1] = i .. " " .. tostring(address)
end
crash.release(handle)
local report = {
title = "Native crash, signal " .. signum .. " (" .. system .. ", Defold " .. engine .. ")",
description = table.concat(lines, "\n"),
category = "crash",
priority = "critical",
auto_captured = true,
metadata = { level = level, defold = engine },
}
if version and version ~= "" then
report.game_version = version
end
queue[#queue + 1] = report
end
return M
The dump describes the run that crashed, not the run that is reading it. That is why the game version and level come from the dump’s user fields instead of from the running game: if the player updated between the crash and the next launch, the current version would be the wrong one.
User fields are the breadcrumbs. crash.set_user_field(index, value) gives you 32 slots of up to 255 characters, and whatever they held at the moment of the crash is stored in the dump. Slot 0 here is the current level, updated through bugreport.set_level() whenever you load a collection. Slot 1 is the game version. A crash that says “signal 11 in level_boss_3 on 1.2.0” is a lead. A crash that says “signal 11” is not.
Be realistic about the backtrace. crash.get_backtrace returns raw addresses. Turning them into function names needs the debug symbols for that exact engine build and for any native extensions you bundled, so keep the symbols from every release build. crash.get_extra_data returns platform-specific text that sometimes includes readable frames, which is why the report includes it in full. Test the whole path on every platform you ship, since crash handling differs from one platform to the next.
Wire it up from a script
Put a game object with this script in your bootstrap collection so it starts as early as possible and stays loaded for the whole session:
-- main/bugreport.script
local bugreport = require "main.bugreport"
function init(self)
bugreport.check_previous_crash()
bugreport.install()
if crash then
crash.set_user_field(1, sys.get_config_string("project.version", "0"))
end
end
function update(self, dt)
bugreport.flush()
end
flush sends at most one queued report per frame, from a script’s update. To test it, add local x = nil; print(x.y) to any script’s update, run the game, and check that exactly one report arrives however long you leave it running. Then call bugreport.set_level("level_1") from your level loader and confirm the level shows up in the report metadata.
Letting players report a problem
Automatic reports cover errors. They do not cover a jump that cannot be made or a sprite drawn behind the background. Defold has no ready-made text field, so the quickest useful version is a small panel of fixed reasons. Each button sends a report with the reason, the level and the last script error.
-- main/report.gui_script
local bugreport = require "main.bugreport"
local REASONS = {
stuck = "I am stuck",
visual = "Something looks wrong",
slow = "The game is slow or freezing",
}
function init(self)
msg.post(".", "acquire_input_focus")
end
function on_input(self, action_id, action)
if action_id ~= hash("touch") or not action.pressed then
return
end
for node_id, reason in pairs(REASONS) do
if gui.pick_node(gui.get_node(node_id), action.x, action.y) then
bugreport.send({
title = reason .. " (" .. bugreport.level .. ")",
description = reason .. "\nLevel: " .. bugreport.level
.. "\nLast script error: " .. bugreport.last_error,
}, function(ok)
gui.set_text(gui.get_node("status"), ok and "Thanks, sent." or "Could not send.")
end)
return true
end
end
end
The GUI scene needs three box nodes with the ids stuck, visual and slow, and a text node called status. Fixed reasons have a side benefit: because the title is “I am stuck (level_4)”, every player who presses that button in level 4 lands on the same report, and the occurrence count becomes a heat map of where your game is too hard or broken.
If you want a typed description, add a text trigger to your input bindings and append action.text to a string in on_input, or use a GUI library that ships an input field. On mobile you also need gui.show_keyboard. A screenshot is not available from the core API; native extensions exist for it if you need one.
HTML5, mobile and rate limits
HTML5. http.request works in the browser, and the request is cross-origin from wherever the game is hosted. The server must allow that. Bugnet’s report endpoint accepts browser requests from any origin, so builds on itch.io or your own site work unchanged. sys.get_sys_info() also returns a user_agent field on HTML5, which is worth adding to the metadata because “HTML5” alone does not tell you the browser.
Mobile. device_model and manufacturer are filled in on iOS and Android, which is why the module builds device_info from them. On desktop they are empty.
Rate limits. Any ingestion endpoint limits requests per client. One report per frame from flush, a cap of five automatic reports and one manual report per button press stay far below any reasonable limit. If a request fails because the player is offline, this module drops it. That is a fair trade for a first version: the same error will arrive from the next player who is online.
Where the reports go
The module posts to Bugnet’s public report API: POST /api/bugs/submit with the project key in X-API-Key and a JSON body. There is no Defold SDK to install. The key is an ingestion key, safe to ship in your Lua source, because it can file reports but cannot read them.
In the dashboard, reports with the same title stack into one entry with an occurrence count, and each occurrence keeps its own platform, version and metadata. A Lua error on line 42 that has hit 300 players sits above one that has hit 3, without you sorting anything. Native crash reports arrive with the dump text in the description. From there you can assign and prioritize reports, send new ones to Discord, and publish fixed issues to a public tracker and changelog. The free plan covers one project and up to 100 bug reports; the quickstart shows where to find the key, and the API explorer lets you try the request before writing any Lua.
Related guides
- Crash Reporting for LÖVE (love2d) Games
- A Bug Report API for Games: Submit Reports from Launchers, Servers and Tools
- What Is a Native Crash vs a Managed Crash?
- How to Symbolicate Native Crashes in a Game
- Should I Build My Own Crash Reporter?
Frequently asked questions
How do I catch Lua errors in a Defold game?
Call sys.set_error_handler with a function that takes source, message and traceback. Defold calls it whenever a script raises an error. The message contains the file, line and error text, and the traceback is the Lua stack.
Does Defold have built-in crash reporting?
Defold has a crash module that records a small dump when the engine dies in native code and lets you read it on the next start with crash.load_previous. It does not send the dump anywhere. Uploading it to a server is your job.
Why does my Defold error handler fire hundreds of times?
An error inside update or another per-frame callback is raised again on every frame, so the handler runs sixty times a second. Remember each message you have already handled and ignore repeats, and cap the number of reports per session.
Can I turn crash.get_backtrace addresses into function names?
Not from inside the game. The backtrace is a list of raw addresses, so you need the debug symbols for the exact engine build and any native extensions you bundled. Keep the symbols for every release, and send crash.get_extra_data too, since it sometimes carries readable frames.
Is there a Bugnet SDK for Defold?
No. Defold games use Bugnet’s public HTTP API, a single POST with an X-API-Key header and a JSON body. http.request and json.encode from the Defold standard API are all it takes.
Defold gives you the two hooks, one for Lua errors and one for native crashes. The work is deciding what to send once, what to drop, and sending it from a script instead of from the handler.