Disable on Error

Posted by Rene on Sun 08 Jul 2018 05:48 PM — 4 posts, 16,571 views.

#0
Is there a way to make a plugin automatically disable itself on an error? I find ones with timers get insanely spammy when they error if it is updating frequently until you get a chance to manually disable it, is there some internal way to detect if it has an error and disable itself?

Thanks.
Australia Forum Administrator #1
What do you mean by "an error"? A script error? Do you have your scripts in "send to script" or a script file? I think you'll find that if you use a script file the plugin gets automatically disabled when an error is raised.
#2
I meant a script error in a plugin.
I find that if that function is called again like by a tic timer it will keep displaying that the function had a previous error and can be real spammy, is there a way to just have it disable the plugin?
Australia Forum Administrator #3
There is a thing called a "protected call" (pcall) which you can use here. This is an example plugin:


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Failed_Plugin_Test"
   author="Nick Gammon"
   id="35cdfbf0c4bd7c0c58409408"
   language="Lua"
   purpose="testing auto-disable plugins"
   date_written="2018-07-10 08:35:14"
   requires="5.06"
   version="1.0"
   >

</plugin>


<!--  Timers  -->

<timers>
  <timer enabled="y" 
    second="3.00" 
    name="periodic_timer"
    send_to="12"
>

<send>

local function protected_timer_function ()
   print "timer test"
   error "plugin error"
end -- function protected_call

ok, result = pcall (protected_timer_function)
if ok then
  return
end -- if ok

-- disable this timer (or the plugin if you want)
EnableTimer ("periodic_timer", false)
-- show the error
error (result)

</send>

  </timer>
</timers>

</muclient>


In this case I have a timer that fires every 3 seconds, however it has an error in it: error "plugin error"

It is called by the timer with a "pcall" which can detect if there was an error in that function (or any function that it calls). If so, the "ok" result is nil, and we can use that to detect that an error occurred, and then disable the timer (or you could disable the entire plugin) so as to reduce spam.