Commit f0db8221 authored by Linus Pithan's avatar Linus Pithan
Browse files

finetuning of error messages displayed on ModuleNotFoundError

parent 306fc353
Pipeline #20044 passed with stages
in 35 minutes and 41 seconds
......@@ -7,6 +7,7 @@
from bliss.config.static import get_config
import re
from importlib.util import find_spec
def camel_case_to_snake_style(name):
......@@ -34,16 +35,21 @@ def find_class_and_node(cfg_node, base_path="bliss.controllers"):
try:
module = __import__(module_name, fromlist=[""])
except ModuleNotFoundError as e:
if find_spec(module_name) is not None:
raise e
module_name = "%s.%s" % (base_path, camel_case_to_snake_style(klass_name))
try:
module = __import__(module_name, fromlist=[""])
except ModuleNotFoundError as e2:
msg = "\nWITH CONFIG MODULE NAME: " + e.msg
msg += "\nWITH DEFAULT MODULE NAME: " + e2.msg
msg += f"\nCHECK THAT MODULE NAME BEGINS AFTER '{base_path}'\n"
e2.msg = msg
raise e2
except ModuleNotFoundError as e2:
if find_spec(module_name) is not None:
raise e2
else:
msg = "CONFIG COULD NOT FIND CLASS!"
msg += "\nWITH CONFIG MODULE NAME: " + e.msg
msg += "\nWITH DEFAULT MODULE NAME: " + e2.msg
msg += f"\nCHECK THAT MODULE NAME BEGINS AFTER '{base_path}'\n"
raise ModuleNotFoundError(msg)
try:
klass = getattr(module, klass_name)
......
......@@ -215,3 +215,18 @@ def test_capital_letter_file(beacon):
# returned as expected
x = beacon.get("Aunused")
assert x
def test_bliss_import_error(beacon):
with pytest.raises(RuntimeError) as excinfo:
beacon.get("broken_ctrl3")
# Non existing class
with pytest.raises(ModuleNotFoundError) as excinfo:
beacon.get("broken_ctrl")
assert "CONFIG COULD NOT FIND CLASS" in str(excinfo.value)
# faulty import in imported module
with pytest.raises(ModuleNotFoundError) as excinfo:
beacon.get("broken_ctrl2")
assert "CONFIG COULD NOT FIND CLASS" not in str(excinfo.value)
import ppprint # type to make import of this module fail
class SomeClass(object):
def __init__(self, name, config):
print("SomeOutput")
- plugin: bliss
module: not_exixting_module
class: NoClass
name: broken_ctrl
- plugin: bliss
package: tests.working
class: SomeClass
name: working_ctrl
- plugin: bliss
package: tests.not_working
class: SomeClass
name: broken_ctrl2
- plugin: bliss
package: tests.working
class: SomeClass2
name: broken_ctrl3
import pprint
class SomeClass(object):
def __init__(self, name, config):
print("SomeOutput")
class SomeClass2(object):
def __init__(self, name, config):
raise RuntimeError
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment