Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhelpful express error message: Error loading ASGI app. Attribute not found in module "shiny.express.app" #869

Closed
cpsievert opened this issue Dec 7, 2023 · 2 comments · Fixed by #888
Labels

Comments

@cpsievert
Copy link
Collaborator

At least one way to repro this is to run an app that attempts to import something that doesn't exist in shiny.express.layout

from shiny.express import layout

print(layout.foo)
@wch
Copy link
Collaborator

wch commented Dec 9, 2023

This is the error message:

ERROR:    Error loading ASGI app. Attribute "_2f_Users_2f_winston_2f_Projects_2f_py_2d_shiny_2f_examples_2f_express_2f_plot_5f_app_2e_py" not found in module "shiny.express.app".

Interestingly, I found that this happens only if an AttributeError is raised, as in:

from shiny import express

raise AttributeError("This is an error")

For other types of errors, like ValueError("This is an error"), you get a normal stack trace, like:

Traceback (most recent call last):
...
...
  File "/Users/winston/Projects/py-shiny/shiny/express/_run.py", line 44, in wrap_express_app
    app_ui = run_express(file)
             ^^^^^^^^^^^^^^^^^
  File "/Users/winston/Projects/py-shiny/shiny/express/_run.py", line 96, in run_express
    exec(
  File "/Users/winston/Projects/py-shiny/examples/express/plot_app.py", line 17, in <module>
    raise ValueError("This is an error")
ValueError: This is an error

In Shiny, we're not doing any special handling of AttributeError, so I think uvicorn is probably catching that specific kind of error and handling it differently.

@wch
Copy link
Collaborator

wch commented Dec 9, 2023

This is where uvicorn is catching it:

https://github.com/encode/uvicorn/blob/c55af77fe9a4da99ac594e15ab9ee07b34ef669c/uvicorn/importer.py#L29-L36

    try:
        for attr_str in attrs_str.split("."):
            instance = getattr(instance, attr_str)
    except AttributeError:
        message = 'Attribute "{attrs_str}" not found in module "{module_str}".'
        raise ImportFromStringError(
            message.format(attrs_str=attrs_str, module_str=module_str)
        )

I think the best course of action is to catch that kind of error and raise a different one that uvicorn won't catch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants