Skip to content

Better messages for AttributeError in Shiny Express #888

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

Merged
merged 2 commits into from
Dec 11, 2023
Merged

Conversation

wch
Copy link
Collaborator

@wch wch commented Dec 9, 2023

This closes #869.

Previously in the case of an AttributeError, it would print this:

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".

With this PR, it will print a normal stack trace and then a RuntimeError:

  ...
  File "/Users/winston/Library/py-shiny/shiny/express/app.py", line 13, in __getattr__
    return wrap_express_app(Path(name))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/winston/Library/py-shiny/shiny/express/_run.py", line 44, in wrap_express_app
    app_ui = run_express(file)
             ^^^^^^^^^^^^^^^^^
  File "/Users/winston/Library/py-shiny/shiny/express/_run.py", line 129, in run_express
    raise RuntimeError(e)
RuntimeError: This is an error

This PR wraps the bulk of the run_express function in a try statement. I also added a bit of code to preserve the previous sys.displayhook, and then restore it in the finally. This is helpful when running multiple concurrent apps, to ensure that sys.displayhook isn't left in an intermediate state.

To see the actual substantive changes (and not just indentation change), turn on "Hide whitespace".

@wch wch requested a review from jcheng5 December 9, 2023 04:37
@wch
Copy link
Collaborator Author

wch commented Dec 11, 2023

Update: I added exception chaining (raise RuntimeError(e) from e), and now the output looks like this:

Traceback (most recent call last):
  File "/Users/winston/Projects/py-shiny/shiny/express/_run.py", line 98, in run_express
    exec(
  File "/Users/winston/Projects/py-shiny/examples/express/plot_app.py", line 17, in <module>
    render.asdf
AttributeError: module 'shiny.render' has no attribute 'asdf'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/winston/Projects/py-shiny/venv/bin/shiny", line 33, in <module>
    sys.exit(load_entry_point('shiny', 'console_scripts', 'shiny')())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/winston/Projects/py-shiny/venv/lib/python3.11/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
 ...
 ...
  File "/Users/winston/Projects/py-shiny/shiny/express/app.py", line 13, in __getattr__
    return wrap_express_app(Path(name))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  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 129, in run_express
    raise RuntimeError(e) from e
RuntimeError: module 'shiny.render' has no attribute 'asdf'

@wch wch merged commit 6a28a59 into main Dec 11, 2023
@wch wch deleted the better-attribute-error branch December 11, 2023 18:55
schloerke added a commit that referenced this pull request Dec 18, 2023
* main: (24 commits)
  docs: Use definition lists for parameter descriptions to enable full markdown support (#901)
  docs(layout_columns): Add example app (#903)
  fix(layout_columns): Remove use of enum for breakpoints (#912)
  Express: don't automatically put strings in a `<pre>` block (#905)
  Deprecate `nav()` in favor of `nav_panel()` (#876)
  chore: Remove unused import
  refactor(layout_column_wrap): Use helper for wrapping children (#910)
  fix(layout_columns): Don't apply fillable class to layout container (#909)
  Add output to React template (#908)
  fix(layout_columns): Add missing `bslib-mb-spacing` class (#906)
  Custom component template updates (#874)
  fix(layout_columns): If the breakpoint name is an enum, need to collect the value (#902)
  chore: exclude `_dev/` folder (#897)
  feat: Add `ui.layout_columns()` (#856)
  Update VS Code settings file
  Express: change default page function to page_fixed (#892)
  Better messages for `AttributeError` in Shiny Express (#888)
  Fix 0.6.0 release date
  Fix failing express tests (#887)
  Explain why function is called twice
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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