init.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. if core.cause_error == nil or
  22. type(core.cause_error) ~= "function" then
  23. return
  24. end
  25. core.log("action", "WARNING: loading exception handler test module!")
  26. local exceptiondata = {
  27. tocause = "none",
  28. mapgen = false,
  29. entity_step = false,
  30. globalstep = false,
  31. }
  32. local exception_entity =
  33. {
  34. on_step = function(self, dtime)
  35. if exceptiondata.entity_step then
  36. core.cause_error(exceptiondata.tocause)
  37. end
  38. end,
  39. }
  40. local exception_entity_name = "errorhandler_test:error_entity"
  41. local function exception_chat_handler(playername, param)
  42. local parameters = param:split(" ")
  43. if #parameters ~= 2 then
  44. core.chat_send_player(playername, "Invalid argument count for exceptiontest")
  45. end
  46. core.log("error", "Causing error at:" .. parameters[1])
  47. if parameters[1] == "mapgen" then
  48. exceptiondata.tocause = parameters[2]
  49. exceptiondata.mapgen = true
  50. elseif parameters[1] == "entity_step" then
  51. --spawn entity at player location
  52. local player = core.get_player_by_name(playername)
  53. if player:is_player() then
  54. local pos = player:getpos()
  55. core.add_entity(pos, exception_entity_name)
  56. end
  57. exceptiondata.tocause = parameters[2]
  58. exceptiondata.entity_step = true
  59. elseif parameters[1] == "globalstep" then
  60. exceptiondata.tocause = parameters[2]
  61. exceptiondata.globalstep = true
  62. elseif parameters[1] == "immediate" then
  63. core.cause_error(parameters[2])
  64. else
  65. core.chat_send_player(playername, "Invalid error location: " .. dump(parameters[1]))
  66. end
  67. end
  68. core.register_chatcommand("exceptiontest",
  69. {
  70. params = "<location> <errortype>",
  71. description = "cause a given error to happen.\n" ..
  72. " location=(mapgen,entity_step,globalstep,immediate)\n" ..
  73. " errortype=(segv,zerodivision,exception)",
  74. func = exception_chat_handler,
  75. privs = { server=true }
  76. })
  77. core.register_globalstep(function(dtime)
  78. if exceptiondata.globalstep then
  79. core.cause_error(exceptiondata.tocause)
  80. end
  81. end)
  82. core.register_on_generated(function(minp, maxp, blockseed)
  83. if exceptiondata.mapgen then
  84. core.cause_error(exceptiondata.tocause)
  85. end
  86. end)
  87. core.register_entity(exception_entity_name, exception_entity)