Skip to content

Commit b8fe11f

Browse files
authored
JIRA WDT-638 - Should never exit with uncaught exceptions (#1157)
1 parent 11b3973 commit b8fe11f

File tree

13 files changed

+122
-13
lines changed

13 files changed

+122
-13
lines changed

core/src/main/python/compare_model.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#
1212
# If the flag is not provided then all output is written to the standard out.
1313
#
14+
import exceptions
1415
import os
1516
import sets
1617
import sys
@@ -348,4 +349,9 @@ def format_message(key, *args):
348349

349350

350351
if __name__ == "__main__":
351-
main()
352+
try:
353+
main()
354+
except exceptions.SystemExit, ex:
355+
raise ex
356+
except (exceptions.Exception, java.lang.Exception), ex:
357+
exception_helper.__handle_unexpected_exception(ex, _program_name, _class_name, _logger)

core/src/main/python/create.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
55
The main module for the WLSDeploy tool to create empty domains.
66
"""
7+
import exceptions
78
import os
89
import sys
10+
911
from java.io import IOException
1012
from java.lang import IllegalArgumentException
1113
from java.lang import String
@@ -376,4 +378,9 @@ def main(args):
376378

377379
if __name__ == '__main__' or __name__ == 'main':
378380
WebLogicDeployToolingVersion.logVersionInfo(_program_name)
379-
main(sys.argv)
381+
try:
382+
main(sys.argv)
383+
except exceptions.SystemExit, ex:
384+
raise ex
385+
except (exceptions.Exception, java.lang.Exception), ex:
386+
exception_helper.__handle_unexpected_exception(ex, _program_name, _class_name, __logger)

core/src/main/python/deploy.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
The entry point for the deployApps tool.
66
"""
7+
import exceptions
78
import os
89
import sys
910

@@ -17,6 +18,7 @@
1718
# imports from local packages start here
1819
from wlsdeploy.aliases.aliases import Aliases
1920
from wlsdeploy.aliases.wlst_modes import WlstModes
21+
from wlsdeploy.exception import exception_helper
2022
from wlsdeploy.exception.expection_types import ExceptionType
2123
from wlsdeploy.logging.platform_logger import PlatformLogger
2224
from wlsdeploy.tool.deploy import deployer_utils
@@ -258,4 +260,9 @@ def main(args):
258260

259261
if __name__ == '__main__' or __name__ == 'main':
260262
WebLogicDeployToolingVersion.logVersionInfo(_program_name)
261-
main(sys.argv)
263+
try:
264+
main(sys.argv)
265+
except exceptions.SystemExit, ex:
266+
raise ex
267+
except (exceptions.Exception, java.lang.Exception), ex:
268+
exception_helper.__handle_unexpected_exception(ex, _program_name, _class_name, __logger)

core/src/main/python/discover.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
The entry point for the discoverDomain tool.
66
"""
7+
import exceptions
78
import os
89
import sys
910

@@ -234,6 +235,7 @@ def __discover(model_context, aliases, credential_injector, helper):
234235
model = Model()
235236
base_location = LocationContext()
236237
__connect_to_domain(model_context, helper)
238+
237239
try:
238240
_add_domain_name(base_location, aliases, helper)
239241
DomainInfoDiscoverer(model_context, model.get_model_domain_info(), base_location, wlst_mode=__wlst_mode,
@@ -367,6 +369,7 @@ def __disconnect_domain(helper):
367369
_method_name = '__disconnect_domain'
368370

369371
__logger.entering(class_name=_class_name, method_name=_method_name)
372+
370373
if __wlst_mode == WlstModes.ONLINE:
371374
try:
372375
helper.disconnect()
@@ -571,7 +574,6 @@ def main(args):
571574
:return:
572575
"""
573576
_method_name = 'main'
574-
575577
__logger.entering(class_name=_class_name, method_name=_method_name)
576578
for index, arg in enumerate(args):
577579
__logger.finer('sys.argv[{0}] = {1}', str(index), str(arg), class_name=_class_name, method_name=_method_name)
@@ -638,4 +640,9 @@ def main(args):
638640

639641
if __name__ == '__main__' or __name__ == 'main':
640642
WebLogicDeployToolingVersion.logVersionInfo(_program_name)
641-
main(sys.argv)
643+
try:
644+
main(sys.argv)
645+
except exceptions.SystemExit, ex:
646+
raise ex
647+
except (exceptions.Exception, java.lang.Exception), ex:
648+
exception_helper.__handle_unexpected_exception(ex, _program_name, _class_name, __logger)

core/src/main/python/encrypt.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
The main module for the WLSDeploy tool to encrypt passwords.
66
"""
7+
import exceptions
78
import sys
89

910
from java.io import IOException
@@ -253,4 +254,9 @@ def main(args):
253254

254255
if __name__ == '__main__' or __name__ == 'main':
255256
WebLogicDeployToolingVersion.logVersionInfo(_program_name)
256-
main(sys.argv)
257+
try:
258+
main(sys.argv)
259+
except exceptions.SystemExit, ex:
260+
raise ex
261+
except (exceptions.Exception, java.lang.Exception), ex:
262+
exception_helper.__handle_unexpected_exception(ex, _program_name, _class_name, __logger)

core/src/main/python/extract_resource.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
The entry point for the extractDomainResource tool.
66
"""
7+
import exceptions
78
import sys
89

910
from oracle.weblogic.deploy.deploy import DeployException
@@ -158,4 +159,9 @@ def main(args):
158159

159160
if __name__ == '__main__' or __name__ == 'main':
160161
WebLogicDeployToolingVersion.logVersionInfo(_program_name)
161-
main(sys.argv)
162+
try:
163+
main(sys.argv)
164+
except exceptions.SystemExit, ex:
165+
raise ex
166+
except (exceptions.Exception, java.lang.Exception), ex:
167+
exception_helper.__handle_unexpected_exception(ex, _program_name, _class_name, __logger)

core/src/main/python/model_help.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
55
The entry point for the modelHelp tool.
66
"""
7+
import exceptions
78
import os
89
import sys
10+
911
from oracle.weblogic.deploy.util import CLAException
1012
from oracle.weblogic.deploy.util import WebLogicDeployToolingVersion
1113

@@ -133,4 +135,9 @@ def main(args):
133135

134136
if __name__ == '__main__' or __name__ == 'main':
135137
WebLogicDeployToolingVersion.logVersionInfo(_program_name)
136-
main(sys.argv)
138+
try:
139+
main(sys.argv)
140+
except exceptions.SystemExit, ex:
141+
raise ex
142+
except (exceptions.Exception, java.lang.Exception), ex:
143+
exception_helper.__handle_unexpected_exception(ex, _program_name, _class_name, __logger)

core/src/main/python/prepare_model.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
#
88
# This code prepare a list of models for deploying to WebLogic Kubernetes Operator Environment.
99

10+
import exceptions
11+
import sys
1012
import traceback
1113

12-
import sys
1314
from oracle.weblogic.deploy.util import CLAException
1415
from oracle.weblogic.deploy.util import PyWLSTException
1516
from oracle.weblogic.deploy.util import WebLogicDeployToolingVersion
1617

1718
from oracle.weblogic.deploy.prepare import PrepareException
19+
from wlsdeploy.exception import exception_helper
1820
from wlsdeploy.logging.platform_logger import PlatformLogger
1921
from wlsdeploy.tool.prepare.model_preparer import ModelPreparer
2022
from wlsdeploy.tool.util import model_context_helper
@@ -109,4 +111,9 @@ def main():
109111

110112
if __name__ == "__main__" or __name__ == 'main':
111113
WebLogicDeployToolingVersion.logVersionInfo(_program_name)
112-
main()
114+
try:
115+
main()
116+
except exceptions.SystemExit, ex:
117+
raise ex
118+
except (exceptions.Exception, java.lang.Exception), ex:
119+
exception_helper.__handle_unexpected_exception(ex, _program_name, _class_name, __logger)

core/src/main/python/update.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
The entry point for the updateDomain tool.
66
"""
7+
import exceptions
78
import os
89
import sys
910

@@ -309,4 +310,9 @@ def main(args):
309310

310311
if __name__ == '__main__' or __name__ == 'main':
311312
WebLogicDeployToolingVersion.logVersionInfo(_program_name)
312-
main(sys.argv)
313+
try:
314+
main(sys.argv)
315+
except exceptions.SystemExit, ex:
316+
raise ex
317+
except (exceptions.Exception, java.lang.Exception), ex:
318+
exception_helper.__handle_unexpected_exception(ex, _program_name, _class_name, __logger)

core/src/main/python/validate.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
55
The WLS Deploy tooling entry point for the validateModel tool.
66
"""
7+
78
import copy
9+
import exceptions
810
import sys
11+
912
from java.util.logging import Level
1013

1114
from oracle.weblogic.deploy.logging import WLSDeployLogEndHandler
@@ -196,4 +199,9 @@ def main(args):
196199

197200
if __name__ == '__main__' or __name__ == 'main':
198201
WebLogicDeployToolingVersion.logVersionInfo(_program_name)
199-
main(sys.argv)
202+
try:
203+
main(sys.argv)
204+
except exceptions.SystemExit, ex:
205+
raise ex
206+
except (exceptions.Exception, java.lang.Exception), ex:
207+
exception_helper.__handle_unexpected_exception(ex, _program_name, _class_name, __logger)

core/src/main/python/variable_inject.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
The entry point for the injectVariables tool.
66
"""
7+
import exceptions
78
import sys
89

910
from java.io import File
@@ -204,4 +205,9 @@ def main(args):
204205

205206
if __name__ == '__main__' or __name__ == 'main':
206207
WebLogicDeployToolingVersion.logVersionInfo(_program_name)
207-
main(sys.argv)
208+
try:
209+
main(sys.argv)
210+
except exceptions.SystemExit, ex:
211+
raise ex
212+
except (exceptions.Exception, java.lang.Exception), ex:
213+
exception_helper.__handle_unexpected_exception(ex, _program_name, _class_name, __logger)

core/src/main/python/wlsdeploy/exception/exception_helper.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import oracle.weblogic.deploy.yaml.YamlException as JYamlException
3232

3333
from wlsdeploy.exception.expection_types import ExceptionType
34+
from wlsdeploy.util.cla_utils import CommandLineArgUtil
35+
from wlsdeploy.util import tool_exit
3436

3537
_EXCEPTION_TYPE_MAP = {
3638
ExceptionType.ALIAS: 'create_alias_exception',
@@ -462,3 +464,35 @@ def _return_exception_params(*args, **kwargs):
462464
arg_list = list(args)
463465
error = kwargs.pop('error', None)
464466
return arg_list, error
467+
468+
def __log_and_exit(logger, exit_code, class_name):
469+
"""
470+
Helper method to log the exiting message and call sys.exit()
471+
:param logger: the logger to use
472+
:param exit_code: the exit code to use
473+
:param class_name: the class name to pass to the logger
474+
"""
475+
logger.exiting(result=exit_code, class_name=class_name, method_name=None)
476+
tool_exit.end(None, exit_code)
477+
478+
def __handle_unexpected_exception(ex, program_name, class_name, logger):
479+
"""
480+
Helper method to log and unexpected exception with exiting message and call sys.exit()
481+
Note that the user sees the 'Unexpected' message along with the exception, but no stack trace.
482+
The stack trace goes to the log
483+
:param ex: the exception thrown
484+
:param program_name: the program where it occurred
485+
:param class_name: the class where it occurred
486+
:param logger: the logger to use
487+
"""
488+
logger.severe('WLSDPLY-20035', program_name, sys.exc_info()[0])
489+
490+
if hasattr(ex, 'stackTrace'):
491+
# this works best for java exceptions, and gets the full stacktrace all the way back to weblogic.WLST
492+
logger.finer('WLSDPLY-20036', program_name, ex.stackTrace)
493+
else:
494+
# this is for Python exceptions
495+
# Note: since this is Python 2, it seems we can only get the traceback object via sys.exc_info, and of course only
496+
# while in the except block handling code
497+
logger.finer('WLSDPLY-20036', program_name, traceback.format_exception(type(ex), ex, sys.exc_info()[2]))
498+
__log_and_exit(logger, CommandLineArgUtil.PROG_ERROR_EXIT_CODE, class_name)

core/src/main/resources/oracle/weblogic/deploy/messages/wlsdeploy_rb.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,8 @@ WLSDPLY-20031={0} specified Variable File {1} is not a valid file: {2}
16761676
WLSDPLY-20032=No model specified and no model in archive, no validation performed
16771677
WLSDPLY-20033=Applying filter with name "{0}"
16781678
WLSDPLY-20034=Applying filter with ID "{0}"
1679+
WLSDPLY-20035={0} encountered an unexpected runtime exception. Please file an issue on GitHub and attach the log file and stdout. Exception: {1}
1680+
WLSDPLY-20036={0} encountered an unexpected runtime exception. Stacktrace: {1}
16791681

16801682
# Messages for internal filters
16811683
WLSDPLY-20201=Unsupported attribute {0} at location {1} removed from model

0 commit comments

Comments
 (0)