Don't hide true source of error when catching ImportError #4259

Closed
opened 2025-12-29 18:34:17 +01:00 by adam · 0 comments
Owner

Originally created by @candlerb on GitHub (Nov 9, 2020).

Originally assigned to: @jeremystretch on GitHub.

Environment

  • Python version: 3.6.9
  • NetBox version: 2.9.8

Proposed Functionality

There are a number of places which catch ImportError and replace with a simplified error message, e.g. in authentication.py:

        try:
            from netbox import ldap_config
        except ImportError:
            raise ImproperlyConfigured(
                "ldap_config.py does not exist"
            )

This can give an incorrect diagnosis of the problem, which leads to hard-to-debug problems, especially when trying to support users remotely.

I would propose either:

  1. Don't catch the ImportError at all. The backtrace will still say ModuleNotFoundError: No module named 'ldap_config' which is pretty obvious.

  2. Catch it, but include the underlying error:

            try:
                from netbox import ldap_config
            except ImportError as err:
                raise ImproperlyConfigured(
                    "ldap_config.py does not exist or other problem: %s" % err
                )
    

    (also it may be better to catch ModuleNotFoundError, which is a subclass of ImportError)

  3. Catch it, but check for the exact condition before making a summary diagnosis

            try:
                from netbox import ldap_config
            except ModuleNotFoundError as err:
                if getattr(err, "name") == "ldap_config":
                    raise ImproperlyConfigured(
                        "ldap_config.py does not exist"
                    )
                raise
    

Use Case

A real-world case from the forum. A user created ldap_config.py which in turn contained an import error. However Netbox then insisted that the error is "ldap_config.py does not exist" which was not true, hiding the actual error. The user went on a wild goose chase of making additional copies of ldap_config.py in various places.

Database Changes

None

External Dependencies

None

Originally created by @candlerb on GitHub (Nov 9, 2020). Originally assigned to: @jeremystretch on GitHub. ### Environment * Python version: 3.6.9 * NetBox version: 2.9.8 ### Proposed Functionality There are a number of places which catch ImportError and replace with a simplified error message, e.g. in [authentication.py](https://github.com/netbox-community/netbox/blob/develop/netbox/netbox/authentication.py#L147-L152): ``` try: from netbox import ldap_config except ImportError: raise ImproperlyConfigured( "ldap_config.py does not exist" ) ``` This can give an incorrect diagnosis of the problem, which leads to hard-to-debug problems, especially when trying to support users remotely. I would propose either: 1. Don't catch the ImportError at all. The backtrace will still say `ModuleNotFoundError: No module named 'ldap_config'` which is pretty obvious. 2. Catch it, but include the underlying error: ``` try: from netbox import ldap_config except ImportError as err: raise ImproperlyConfigured( "ldap_config.py does not exist or other problem: %s" % err ) ``` (also it may be better to catch `ModuleNotFoundError`, which is a subclass of `ImportError`) 3. Catch it, but check for the exact condition before making a summary diagnosis ``` try: from netbox import ldap_config except ModuleNotFoundError as err: if getattr(err, "name") == "ldap_config": raise ImproperlyConfigured( "ldap_config.py does not exist" ) raise ``` ### Use Case A real-world case from the [forum](https://groups.google.com/d/topic/netbox-discuss/lXxMQM5w3Tw/discussion). A user created `ldap_config.py` which in turn contained an import error. However Netbox then insisted that the error is "ldap_config.py does not exist" which was not true, hiding the actual error. The user went on a wild goose chase of making additional copies of ldap_config.py in various places. ### Database Changes None ### External Dependencies None
adam added the status: acceptedtype: housekeeping labels 2025-12-29 18:34:17 +01:00
adam closed this issue 2025-12-29 18:34:17 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4259