Skip to content

Commit 2dac8c8

Browse files
committed
Added support for setting a soft and hard button callback to the error handler
1 parent c6dffb4 commit 2dac8c8

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

googleanalytics/tracker.lua

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,23 @@ function M.create(tracking_id)
7575
-- It will also, when called, load any previous hard crash using
7676
-- crash.load_previous() and track that as a fatal crash.
7777
-- @param enabled Set to true to enable automatic crash reporting
78-
function tracker.enable_crash_reporting(enabled)
78+
-- @param on_soft_crash Optional callback to invoke when a soft crash is detected
79+
-- @param on_hard_crash Optional callback to invoke when a hard crash is detected
80+
function tracker.enable_crash_reporting(enabled, on_soft_crash, on_hard_crash)
7981
if enabled then
8082
sys.set_error_handler(function(source, message, traceback)
8183
tracker.exception(message, false)
84+
if on_soft_crash then
85+
on_soft_crash(source, message, traceback)
86+
end
8287
end)
8388
local handle = crash.load_previous()
8489
if handle then
8590
tracker.exception(crash.get_extra_data(handle), true)
8691
crash.release(handle)
92+
if on_hard_crash then
93+
on_hard_crash(handle)
94+
end
8795
end
8896
else
8997
sys.set_error_handler(function() end)

tests/test_tracker.lua

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ return function()
2020
describe("tracker", function()
2121
before(function()
2222
mock_fs.mock()
23+
mock.mock(sys)
2324
tracker = require "googleanalytics.tracker"
2425
queue = require "googleanalytics.internal.queue"
2526

@@ -33,6 +34,7 @@ return function()
3334
after(function()
3435
mock_fs.unmock()
3536
mock.unmock(queue)
37+
mock.unmock(sys)
3638
package.loaded["googleanalytics.tracker"] = nil
3739
package.loaded["googleanalytics.internal.queue"] = nil
3840
end)
@@ -140,18 +142,82 @@ return function()
140142
assert(queue_params[2] == t.base_params .. "&t=timing&utc=category2&utv=variable2&utt=20&utl=label2")
141143
end)
142144

143-
it("should be able to enable automatic crash reporting", function()
145+
it("should be able to enable automatic hard crash reporting", function()
144146
if not crash then
145147
return
146148
end
147149
local t = tracker.create("UA-87977671-1")
148150
crash.write_dump()
149151
t.enable_crash_reporting(true)
150-
152+
153+
assert(#queue_params == 1)
154+
local params = split_params(queue_params[1])
155+
assert(params.exf == "1") -- fatal
156+
assert(params.exd)
157+
end)
158+
159+
it("should be able to enable automatic soft crash reporting", function()
160+
if not crash then
161+
return
162+
end
163+
local soft_crash
164+
sys.set_error_handler.replace(function(handler)
165+
soft_crash = handler
166+
end)
167+
168+
local t = tracker.create("UA-87977671-1")
169+
t.enable_crash_reporting(true)
170+
soft_crash("lua", "message", "traceback")
171+
172+
assert(#queue_params == 1)
173+
local params = split_params(queue_params[1])
174+
assert(params.exf == "0") -- non-fatal
175+
assert(params.exd == "message")
176+
assert(params.exd)
177+
end)
178+
179+
it("should be able to forward hard crashes when automatic crash reporting is enabled", function()
180+
if not crash then
181+
return
182+
end
183+
184+
local on_hard_crash_invoked = false
185+
local function on_hard_crash() on_hard_crash_invoked = true end
186+
187+
local t = tracker.create("UA-87977671-1")
188+
crash.write_dump()
189+
t.enable_crash_reporting(true, nil, on_hard_crash)
190+
151191
assert(#queue_params == 1)
152192
local params = split_params(queue_params[1])
153193
assert(params.exf == "1")
154194
assert(params.exd)
195+
assert(on_hard_crash_invoked)
196+
end)
197+
198+
it("should be able to forward soft crashes when automatic crash reporting is enabled", function()
199+
if not crash then
200+
return
201+
end
202+
203+
local on_soft_crash_invoked = false
204+
local function on_soft_crash() on_soft_crash_invoked = true end
205+
206+
local soft_crash
207+
sys.set_error_handler.replace(function(handler)
208+
soft_crash = handler
209+
end)
210+
211+
local t = tracker.create("UA-87977671-1")
212+
t.enable_crash_reporting(true, on_soft_crash, nil)
213+
soft_crash("lua", "message", "traceback")
214+
215+
assert(#queue_params == 1)
216+
local params = split_params(queue_params[1])
217+
assert(params.exf == "0") -- non-fatal
218+
assert(params.exd == "message")
219+
assert(params.exd)
220+
assert(on_soft_crash_invoked)
155221
end)
156222
end)
157223
end

0 commit comments

Comments
 (0)