bottle-react icon indicating copy to clipboard operation
bottle-react copied to clipboard

Do not silently swallow import errors in non-prod environments

Open logannc opened this issue 6 years ago • 5 comments

try:
      import jsx_props
      self.default_props = jsx_props.__dict__.get('init%s'%name, dict)
    except ImportError:
      pass

Do not silently swallow import errors in non-prod environments.

logannc avatar Jun 12 '18 20:06 logannc

defining jsx_props is optional. what should we do if it's not there?

keredson avatar Jul 17 '18 18:07 keredson

This was poorly explained. The idea was to separate finding jsx_props and loading it so that we don't silence errors in jsx_props itself.

I'm not sure what the best mechanism is, but something like this would work:

import importlib

jsx_props_exists = importlib.util.find_spec('jsx_props')
if jsx_props_exists:
  import jsx_props
  self.default_props = jsx_props.__dict__.get('init%s'%name, dict)

logannc avatar Jul 17 '18 18:07 logannc

ah. yeah, that makes sense

keredson avatar Jul 17 '18 20:07 keredson

it seems that python already does this:

$ python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import bad
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "bad.py", line 4
    def ():
        ^
SyntaxError: invalid syntax
>>> 
>>> import good
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named good
>>> 

note the two different exception types

keredson avatar Jul 17 '18 21:07 keredson

Not if the error in bad is an ImportError.

logan@logan-desktop:~/.../pyapp$ python
Python 3.5.3 (default, Nov 23 2017, 11:34:05) 
[GCC 6.3.0 20170406] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import bad
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/logan/.../pyapp/bad.py", line 1, in <module>
    import idontexist
ImportError: No module named 'idontexist'
>>> 
logan@logan-desktop:~/.../pyapp$ cat bad.py 
import idontexist

logannc avatar Jul 17 '18 21:07 logannc