example howto handle Exceptions in shell
This commit is contained in:
@@ -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:
|
try:
|
||||||
if key not in types:
|
a, kwa = h(*args)
|
||||||
self.raiseTclError('Unknown parameter: %s' % key)
|
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]))
|
||||||
|
|
||||||
|
if name is None:
|
||||||
|
self.raiseTclError('Argument name is missing.')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
kwa[key] = types[key](kwa[key])
|
obj = self.collection.get_by_name(str(name))
|
||||||
except Exception, e:
|
except:
|
||||||
self.raiseTclError("Cannot cast argument '%s' to type %s." % (key, types[key]))
|
self.raiseTclError("Could not retrieve object: %s" % name)
|
||||||
|
|
||||||
if name is None:
|
if obj is None:
|
||||||
self.raiseTclError('Argument name is missing.')
|
self.raiseTclError("Object not found: %s" % name)
|
||||||
|
|
||||||
try:
|
if not isinstance(obj, Geometry):
|
||||||
obj = self.collection.get_by_name(str(name))
|
self.raiseTclError('Expected Geometry, got %s %s.' % (name, type(obj)))
|
||||||
except:
|
|
||||||
self.raiseTclError("Could not retrieve object: %s" % name)
|
|
||||||
|
|
||||||
if obj is None:
|
def geo_init(geo_obj, app_obj):
|
||||||
self.raiseTclError("Object not found: %s" % name)
|
geo_obj.solid_geometry = obj_interiors
|
||||||
|
|
||||||
if not isinstance(obj, Geometry):
|
if 'outname' in kwa:
|
||||||
self.raiseTclError('Expected Geometry, got %s %s.' % (name, type(obj)))
|
outname = kwa['outname']
|
||||||
|
else:
|
||||||
|
outname = name + ".interiors"
|
||||||
|
|
||||||
def geo_init(geo_obj, app_obj):
|
try:
|
||||||
geo_obj.solid_geometry = obj_interiors
|
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:
|
except Exception as unknown:
|
||||||
outname = kwa['outname']
|
self.raiseTclUnknownError(unknown)
|
||||||
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))
|
|
||||||
|
|
||||||
return 'Ok'
|
return 'Ok'
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user