Skip to content

Commit e09293b

Browse files
sapiersapier
sapier
authored and
sapier
committedAug 23, 2014
Add lua exception handling test code
Catch some error situations when mod used without thinking about it
1 parent 3e267a6 commit e09293b

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
22+
if core.cause_error == nil or
23+
type(core.cause_error) ~= "function" then
24+
return
25+
end
26+
27+
28+
core.log("action", "WARNING: loading exception handler test module!")
29+
30+
local exceptiondata = {
31+
tocause = "none",
32+
mapgen = false,
33+
entity_step = false,
34+
globalstep = false,
35+
}
36+
37+
local 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+
}
45+
local exception_entity_name = "errorhandler_test:error_entity"
46+
47+
local 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
82+
end
83+
84+
core.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+
94+
core.register_globalstep(function(dtime)
95+
if exceptiondata.globalstep then
96+
core.cause_error(exceptiondata.tocause)
97+
end
98+
end)
99+
100+
core.register_on_generated(function(minp, maxp, blockseed)
101+
if exceptiondata.mapgen then
102+
core.cause_error(exceptiondata.tocause)
103+
end
104+
end)
105+
106+
core.register_entity(exception_entity_name, exception_entity)

‎src/script/lua_api/l_server.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2424
#include "server.h"
2525
#include "environment.h"
2626
#include "player.h"
27+
#include "log.h"
2728

2829
// request_shutdown()
2930
int ModApiServer::l_request_shutdown(lua_State *L)
@@ -449,6 +450,36 @@ int ModApiServer::l_notify_authentication_modified(lua_State *L)
449450
return 0;
450451
}
451452

453+
#ifndef NDEBUG
454+
// cause_error(type_of_error)
455+
int ModApiServer::l_cause_error(lua_State *L)
456+
{
457+
NO_MAP_LOCK_REQUIRED;
458+
std::string type_of_error = "none";
459+
if(lua_isstring(L, 1))
460+
type_of_error = lua_tostring(L, 1);
461+
462+
errorstream << "Error handler test called, errortype=" << type_of_error << std::endl;
463+
464+
if(type_of_error == "segv") {
465+
volatile int* some_pointer = 0;
466+
errorstream << "Cause a sigsegv now: " << (*some_pointer) << std::endl;
467+
468+
} else if (type_of_error == "zerodivision") {
469+
470+
unsigned int some_number = porting::getTimeS();
471+
unsigned int zerovalue = 0;
472+
unsigned int result = some_number / zerovalue;
473+
errorstream << "Well this shouldn't ever be shown: " << result << std::endl;
474+
475+
} else if (type_of_error == "exception") {
476+
throw BaseException("Errorhandler test fct called");
477+
}
478+
479+
return 0;
480+
}
481+
#endif
482+
452483
void ModApiServer::Initialize(lua_State *L, int top)
453484
{
454485
API_FCT(request_shutdown);
@@ -475,4 +506,8 @@ void ModApiServer::Initialize(lua_State *L, int top)
475506
API_FCT(kick_player);
476507
API_FCT(unban_player_or_ip);
477508
API_FCT(notify_authentication_modified);
509+
510+
#ifndef NDEBUG
511+
API_FCT(cause_error);
512+
#endif
478513
}

‎src/script/lua_api/l_server.h

+5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ class ModApiServer : public ModApiBase {
8888
// notify_authentication_modified(name)
8989
static int l_notify_authentication_modified(lua_State *L);
9090

91+
#ifndef NDEBUG
92+
// cause_error(type_of_error)
93+
static int l_cause_error(lua_State *L);
94+
#endif
95+
9196
public:
9297
static void Initialize(lua_State *L, int top);
9398

0 commit comments

Comments
 (0)
Please sign in to comment.