OpenMDAO
OpenMDAO copied to clipboard
Improve exception handling when importing pyoptsparse in pyoptsparse_driver
Summary of Issue
The exception handling in pyoptsparse uses a too general exception handler which could mask error messages that would be useful for debugging.
Issue Type
- [x] Bug
- [ ] Enhancement
POEM #___ <N/A or POEM number at https://github.com/OpenMDAO/POEMs>
Description
The current code is
try:
_tmp = __import__('pyoptsparse', globals(), locals(), [optimizer], 0)
opt = getattr(_tmp, optimizer)()
except Exception as err:
# Change whatever pyopt gives us to an ImportError, give it a readable message,
# but raise with the original traceback.
msg = "Optimizer %s is not available in this installation." % optimizer
raise ImportError(msg)
the proposed solution is something like
try:
_tmp = __import__('pyoptsparse', globals(), locals(), [optimizer], 0)
opt = getattr(_tmp, optimizer)()
except ImportError as err:
# Change whatever pyopt gives us to an ImportError, give it a readable message,
# but raise with the original traceback.
msg = "Optimizer %s is not available in this installation." % optimizer
raise ImportError(msg)
except BaseException as err:
print(f"Unexpected {err=}, {type(err)=}")
raise
Example
N/A
Environment
N/A
I think this was addressed (albeit in a slightly different way than suggested) in #2646
Any error that occurs when importing pyoptsparse and pyoptsparse.Optimization that is not an ImportError will just be re-raised as whatever the error was, with full traceback.
Per the previous comment, I think this was addressed in #2646 and the issue can be closed.