Runtime: Handle errors in user's python code more gracefully : make exceptions and allow repair. svghmi
authorEdouard Tisserant
Thu, 15 Jul 2021 11:50:44 +0200
branchsvghmi
changeset 3282 725d3e9ac913
parent 3281 1fc4274de64e
child 3283 71ae6f02a7ff
Runtime: Handle errors in user's python code more gracefully : make exceptions and allow repair.
Beremiz_service.py
runtime/PLCObject.py
--- a/Beremiz_service.py	Thu Jul 15 11:48:02 2021 +0200
+++ b/Beremiz_service.py	Thu Jul 15 11:50:44 2021 +0200
@@ -31,6 +31,7 @@
 import getopt
 import threading
 import shlex
+import traceback
 from threading import Thread, Semaphore, Lock, currentThread
 from builtins import str as text
 from past.builtins import execfile
@@ -614,8 +615,11 @@
 pyro_thread.join()
 
 plcobj = runtime.GetPLCObjectSingleton()
-plcobj.StopPLC()
-plcobj.UnLoadPLC()
+try:
+    plcobj.StopPLC()
+    plcobj.UnLoadPLC()
+except:
+    print(traceback.format_exc())
 
 if havetwisted:
     reactor.stop()
--- a/runtime/PLCObject.py	Thu Jul 15 11:48:02 2021 +0200
+++ b/runtime/PLCObject.py	Thu Jul 15 11:50:44 2021 +0200
@@ -113,7 +113,9 @@
             if autostart:
                 if self.LoadPLC():
                     self.StartPLC()
-                    return
+                else:
+                    self._fail(_("Problem autostarting PLC : can't load PLC"))
+                return
         except Exception:
             self.PLCStatus = PlcStatus.Empty
             self.CurrentPLCFilename = None
@@ -269,7 +271,12 @@
     def LoadPLC(self):
         res = self._LoadPLC()
         if res:
-            self.PythonRuntimeInit()
+            try:
+                self.PythonRuntimeInit()
+            except Exception:
+                self._loading_error = traceback.format_exc()
+                PLCprint(self._loading_error)
+                return False
         else:
             self._FreePLC()
 
@@ -680,9 +687,9 @@
 
             if self.LoadPLC():
                 self.PLCStatus = PlcStatus.Stopped
+                self.StatusChange()
             else:
-                self.PLCStatus = PlcStatus.Broken
-            self.StatusChange()
+                self._fail(_("Problem installing new PLC : can't load PLC"))
 
             return self.PLCStatus == PlcStatus.Stopped
         return False