Add exception handling to middleware import

Adds a simple try/except block to the middleware import to handle errors
that may result from bad middleware syntax. While this won't allow a
full run to happen, it will at least run preflight/postflight.
This commit is contained in:
Rick Heil
2017-03-02 16:36:53 -05:00
committed by Greg Neagle
parent 0b1a1f344a
commit 003f2489b9
+18 -14
View File
@@ -81,23 +81,27 @@ def import_middleware():
and os.path.splitext(filename)[1] == '.py'):
name = os.path.splitext(filename)[0]
filepath = os.path.join(munki_dir, filename)
_tmp = imp.load_source(name, filepath)
if hasattr(_tmp, required_function_name):
if callable(getattr(_tmp, required_function_name)):
display.display_debug1(
'Loading middleware module %s' % filename)
globals()['middleware'] = _tmp
return
try:
_tmp = imp.load_source(name, filepath)
if hasattr(_tmp, required_function_name):
if callable(getattr(_tmp, required_function_name)):
display.display_debug1(
'Loading middleware module %s' % filename)
globals()['middleware'] = _tmp
return
else:
display.display_warning(
'%s attribute in %s is not callable.'
% (required_function_name, filepath))
display.display_warning('Ignoring %s' % filepath)
else:
display.display_warning(
'%s attribute in %s is not callable.'
% (required_function_name, filepath))
'%s does not have a %s function'
% (filepath, required_function_name))
display.display_warning('Ignoring %s' % filepath)
else:
display.display_warning(
'%s does not have a %s function'
% (filepath, required_function_name))
display.display_warning('Ignoring %s' % filepath)
except:
display.display_warning('Ignoring %s because of error importing module.'
% filepath)
return