django-simple-menu icon indicating copy to clipboard operation
django-simple-menu copied to clipboard

Impossible to debug menu.py errors

Open MarZab opened this issue 9 years ago • 3 comments

menu.py importing has a catch all that hides all errors making you think the plugin is not working when your code is at fault

https://github.com/borgstrom/django-simple-menu/blob/master/menu/menu.py#L75

My snippet:

        try:
            __import__(menu_module, fromlist=["menu", ])
        except ImportError as err:
            traceback.print_tb(err.__traceback__)
            pass

MarZab avatar Mar 02 '16 18:03 MarZab

Hi. Thanks for reporting this. I admit I've been bitten by this before too, so I'm happy to improve our error handling at import.

I will leverage the standard logging package and have it log a clear error along with a full traceback. PR incoming sometime today.

borgstrom avatar Mar 02 '16 19:03 borgstrom

It appears the problem is not that you can't debug all problems when we import the menus, it's that you cannot debug other ImportErrors

If I make a syntax error in the accounts/menus.py file in the example project then I get the correct debug page:

screen shot 2016-03-02 at 8 24 53 pm

However, if I change it so there's a bad import:

diff --git a/example/accounts/menus.py b/example/accounts/menus.py
index 4b0d5b8..e44c03c 100644
--- a/example/accounts/menus.py
+++ b/example/accounts/menus.py
@@ -1,4 +1,4 @@
-from django.core.urlresolvers import reverse
+from django.core.urlresolvers import reverse_NOPE

 from menu import Menu, MenuItem

Then the page loads and there's silently no accounts menu anymore.

Since we also get an ImportError when there's no menus.py file inside an installed app I don't want to make the logging too verbose here.

borgstrom avatar Mar 03 '16 04:03 borgstrom

What if we filter the exceptions to find out where it's coming from? Something along the lines of:

# https://github.com/jazzband/django-simple-menu/blob/21136c30d8dc064f5b587272012301694307c9d5/menu/menu.py#L66
try:
    __import__(menu_module, fromlist=["menu", ])
except ImportError as err:
   if err.name != menu_module:
       raise ImportError from err

This way, importing non-existent app.menus will not throw exception, but incorrect import inside app.menus will

kytta avatar May 08 '22 18:05 kytta