OpenMDAO icon indicating copy to clipboard operation
OpenMDAO copied to clipboard

Improve exception handling when importing pyoptsparse in pyoptsparse_driver

Open hschilling opened this issue 2 years ago • 1 comments

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

hschilling avatar Dec 22 '21 16:12 hschilling

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.

swryan avatar Oct 06 '22 18:10 swryan

Per the previous comment, I think this was addressed in #2646 and the issue can be closed.

swryan avatar Jul 06 '23 18:07 swryan