Explorar o código

example howto handle Exceptions in shell

Kamil Sopko %!s(int64=10) %!d(string=hai) anos
pai
achega
6b527fa256
Modificáronse 1 ficheiros con 55 adicións e 31 borrados
  1. 55 31
      FlatCAMApp.py

+ 55 - 31
FlatCAMApp.py

@@ -644,14 +644,32 @@ class App(QtCore.QObject):
         else:
         else:
             self.defaults['stats'][resource] = 1
             self.defaults['stats'][resource] = 1
 
 
+    class TclErrorException(Exception):
+        """
+        this exception is deffined here, to be able catch it if we sucessfully handle all errors from shell command
+        """
+
+        pass
+
+    def raiseTclUnknownError(self, unknownException):
+        """
+        raise Exception if is different type  than TclErrorException
+        :param unknownException:
+        :return:
+        """
+
+        if not isinstance(unknownException, self.TclErrorException):
+            self.raiseTclError("Unknown error: %s" % str(unknownException))
+
     def raiseTclError(self, text):
     def raiseTclError(self, text):
         """
         """
         this method  pass exception from python into TCL as error, so we get stacktrace and reason
         this method  pass exception from python into TCL as error, so we get stacktrace and reason
         :param text: text of error
         :param text: text of error
         :return: raise exception
         :return: raise exception
         """
         """
+
         self.tcl.eval('return -code error "%s"' % text)
         self.tcl.eval('return -code error "%s"' % text)
-        raise Exception(text)
+        raise self.TclErrorException(text)
 
 
     def exec_command(self, text):
     def exec_command(self, text):
         """
         """
@@ -660,6 +678,7 @@ class App(QtCore.QObject):
         :param text: Input command
         :param text: Input command
         :return: None
         :return: None
         """
         """
+
         self.report_usage('exec_command')
         self.report_usage('exec_command')
 
 
         text = str(text)
         text = str(text)
@@ -2659,44 +2678,49 @@ class App(QtCore.QObject):
             :param args: array of arguments
             :param args: array of arguments
             :return: "Ok" if completed without errors
             :return: "Ok" if completed without errors
             '''
             '''
-            a, kwa = h(*args)
-            types = {'outname': str}
 
 
-            for key in kwa:
-                if key not in types:
-                    self.raiseTclError('Unknown parameter: %s' % key)
-                try:
-                    kwa[key] = types[key](kwa[key])
-                except Exception, e:
-                    self.raiseTclError("Cannot cast argument '%s' to type %s." % (key, types[key]))
+            try:
+                a, kwa = h(*args)
+                types = {'outname': str}
 
 
-            if name is None:
-                self.raiseTclError('Argument name is missing.')
+                for key in kwa:
+                    if key not in types:
+                        self.raiseTclError('Unknown parameter: %s' % key)
+                    try:
+                        kwa[key] = types[key](kwa[key])
+                    except Exception, e:
+                        self.raiseTclError("Cannot cast argument '%s' to type %s." % (key, types[key]))
 
 
-            try:
-                obj = self.collection.get_by_name(str(name))
-            except:
-                self.raiseTclError("Could not retrieve object: %s" % name)
+                if name is None:
+                    self.raiseTclError('Argument name is missing.')
 
 
-            if obj is None:
-                self.raiseTclError("Object not found: %s" % name)
+                try:
+                    obj = self.collection.get_by_name(str(name))
+                except:
+                    self.raiseTclError("Could not retrieve object: %s" % name)
 
 
-            if not isinstance(obj, Geometry):
-                self.raiseTclError('Expected Geometry, got %s %s.' % (name,  type(obj)))
+                if obj is None:
+                    self.raiseTclError("Object not found: %s" % name)
 
 
-            def geo_init(geo_obj, app_obj):
-                geo_obj.solid_geometry = obj_interiors
+                if not isinstance(obj, Geometry):
+                    self.raiseTclError('Expected Geometry, got %s %s.' % (name,  type(obj)))
 
 
-            if 'outname' in kwa:
-                outname = kwa['outname']
-            else:
-                outname = name + ".interiors"
+                def geo_init(geo_obj, app_obj):
+                    geo_obj.solid_geometry = obj_interiors
 
 
-            try:
-                obj_interiors = obj.get_interiors()
-                self.new_object('geometry', outname, geo_init)
-            except Exception as e:
-                self.raiseTclError("Failed: %s" % str(e))
+                if 'outname' in kwa:
+                    outname = kwa['outname']
+                else:
+                    outname = name + ".interiors"
+
+                try:
+                    obj_interiors = obj.get_interiors()
+                    self.new_object('geometry', outname, geo_init)
+                except Exception as e:
+                    self.raiseTclError("Failed: %s" % str(e))
+
+            except Exception as unknown:
+                self.raiseTclUnknownError(unknown)
 
 
             return 'Ok'
             return 'Ok'