1--
2-- exception handler test module
3--
4--
5-- To avoid this from crashing the module will startup in inactive mode.
6-- to make specific errors happen you need to cause them by following
7-- chat command:
8--
9-- exceptiontest <location> <errortype>
10--
11-- location has to be one of:
12--   * mapgen:          cause in next on_generate call
13--   * entity_step:     spawn a entity and make it do error in on_step
14--   * globalstep:      do error in next globalstep
15--   * immediate:       cause right in chat handler
16--
17-- errortypes defined are:
18--   * segv:            make sigsegv happen
19--   * zerodivision:    cause a division by zero to happen
20--   * exception:       throw an exception
21
22if core.cause_error == nil or
23	type(core.cause_error) ~= "function" then
24	return
25end
26
27
28core.log("action", "WARNING: loading exception handler test module!")
29
30local exceptiondata = {
31	tocause = "none",
32	mapgen = false,
33	entity_step = false,
34	globalstep = false,
35}
36
37local exception_entity =
38{
39	on_step = function(self, dtime)
40		if exceptiondata.entity_step then
41			core.cause_error(exceptiondata.tocause)
42		end
43	end,
44}
45local exception_entity_name = "errorhandler_test:error_entity"
46
47local function exception_chat_handler(playername, param)
48	local parameters = param:split(" ")
49
50	if #parameters ~= 2 then
51		core.chat_send_player(playername, "Invalid argument count for exceptiontest")
52	end
53
54	core.log("error", "Causing error at:" .. parameters[1])
55
56	if parameters[1] == "mapgen" then
57		exceptiondata.tocause = parameters[2]
58		exceptiondata.mapgen = true
59	elseif parameters[1] == "entity_step" then
60		--spawn entity at player location
61		local player = core.get_player_by_name(playername)
62
63		if player:is_player() then
64			local pos = player:getpos()
65
66			core.add_entity(pos, exception_entity_name)
67		end
68
69		exceptiondata.tocause = parameters[2]
70		exceptiondata.entity_step = true
71
72	elseif parameters[1] == "globalstep" then
73		exceptiondata.tocause = parameters[2]
74		exceptiondata.globalstep = true
75
76	elseif parameters[1] == "immediate" then
77		core.cause_error(parameters[2])
78
79	else
80		core.chat_send_player(playername, "Invalid error location: " .. dump(parameters[1]))
81	end
82end
83
84core.register_chatcommand("exceptiontest",
85	{
86		params      = "<location> <errortype>",
87		description = "cause a given error to happen.\n" ..
88				" location=(mapgen,entity_step,globalstep,immediate)\n" ..
89				" errortype=(segv,zerodivision,exception)",
90		func        = exception_chat_handler,
91		privs       = { server=true }
92	})
93
94core.register_globalstep(function(dtime)
95	if exceptiondata.globalstep then
96		core.cause_error(exceptiondata.tocause)
97	end
98end)
99
100core.register_on_generated(function(minp, maxp, blockseed)
101	if exceptiondata.mapgen then
102		core.cause_error(exceptiondata.tocause)
103	end
104end)
105
106core.register_entity(exception_entity_name, exception_entity)
107